Matplotlib Cheat Sheet 📊
By: Mulbah Kallen
Tags: Data Visualization, Python, Matplotlib

Making the bar graph horizontal is as easy as plt.barh( ). Let’s add one more attribute to our graphs in order to depict the amount of variance.

Within you code add the following code

varience = [2,4,3,2,4]

plt.barh( sectors , sector_values , xerr = varience , color = ‘blue’)

The xerr= allows us to indicate the amount of variance per sector value. If need be yerr= is also an option.

Next we will create a stacked bar graph. It may appear that there is a lot of code for this graph but try your best to go through it slowly and remember all the steps we took while creating every graph until now.

sectors = [‘Sec 1’,’Sec 2',’Sec 3',’Sec 4',’Sec 5']

sector_values = [ 23 , 45 , 17 , 32 , 29 ]

subsector_values = [ 20 , 40 , 20 , 30 , 30 ] index = np.arange(5)

width = 0.30 plt.bar(index, sector_values, width, color = ‘green’, label = ‘sector_values’) plt.bar(index + width, subsector_values,width, color = ‘blue’, label = ‘subsector_values’) plt.title(‘Horizontally Stacked Bars’)

plt.xlabel(‘Sectors’)

plt.ylabel(‘Sector Values’) plt.xticks(index + width/2 , sectors) plt.legend(loc = ‘best’)

plt.show()

Without making much modification to our code we can stack our bar graphs one atop the other by indicating, for example, bottom = sector_values within the plt.bar() method of the plot that we want to be on top. Be sure to get rid of the width variable and any instance where it was called further down into our code.

index = np.arange( 5 )

plt.bar( index , sector_values , width , color = ‘green’ , label = ‘sector_values’ )

plt.bar( index , subsector_values , width , color = ‘blue’ , label = ‘subsector_values’ , bottom = sector_values )

Next let’s create a pie chart. This is done easily by using the pie( ) method. We will start with a simple chart then add modifying attributes to make it more unique. Again don’t be overwhelmed with the amount of code that this chart requires.

plt.figure( figsize=( 15 , 5 ) )

hospital_dept = [ ‘Dept A’ , ’Dept B’ , ’Dept C’ , ’Dept D’ , ’Dept E’ ]

dept_share = [ 20 , 25 , 15 , 10 , 20 ]

Explode = [ 0 , 0.1 , 0 , 0 , 0 ] — — Explodes the Orange Section of Our Plot

plt.pie( dept_share , explode = Explode , labels = hospital_dept , shadow =’true’ , startangle= 45 )

plt.axis( ‘equal’ )

plt.legend( title = “List of Departmments” , loc=”upper right” )

plt.show( )

Histograms are used to plot the frequency of score occurrences in a continuous dataset that have been divided into classes called bins. In order to create our dataset we are going to use the numpy function np.random.randn. This will generate data with the properties of a normal distribution curve.

x = np.random.randn( 1000 )

plt.title( ‘Histogram’ )

plt.xlabel( ‘Random Data’ )

plt.ylabel( ‘Frequency’ )

plt.hist( x , 10 ) — — — plots our randomly generated x values into 10 bins.

plt.show( )

Finally lets talk about scatter plots and 3D plotting.

Scatter plots are vert useful when dealing with a regression problem. In order to create our scatter plot we are going to create an arbitrary set of height and weight data and plot them against each other.

height = np.array ( [ 192 , 142 , 187 , 149 , 153 , 193 , 155 , 178 , 191 , 177 , 182 , 179 , 185 , 158 , 158 ] )

weight = np.array ( [ 90 , 71 , 66 , 75 , 79 , 60 , 98 , 96 , 68 , 67 , 40 , 68 , 63, 74 , 63 ] )

plt.xlim( 140 , 200 )

plt.ylim( 60 , 100 )

plt.scatter( height , weight )

plt.title( ‘Scatter Plot’ )

plt.xlabel( ‘Height’ )

plt.ylabel( ‘Weight’ )

plt.show( )

This same scatterplot can also be visualized in 3D. To do this we are going to first import the mplot3d module as follows:

from mpl_toolkits import mplot3d

Next we need to create the variable ax that is set equal to our projection type.

ax = plt.axes( projection = ‘3d’)

The following code is fairly repetitive of what you’ve seen before.

ax = plt.axes( projection = ‘3d’ )

ax.scatter3D( height , weight )

ax.set_xlabel( ‘Height’ )

ax.set_ylabel( ‘Weight’ )

plt.show( )

Well if you’ve made it this far you should be proud of yourself. We’ve only gone through the basics of what matplotlib is capable of but, as you’ve noticed, there is a bit of a trend in how plots are created and executed. Check out the Matplotlib Sample Plots page in order to see the many more plots Matplotlib is capable of.

Next we will discuss Seaborn.