We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 4
WA IH
Python For Data Science Cheat Sheet
Matplotlib
Learn Python for Data Science at www.insaid.co
Matplotlib
Matplotlib is a Python 2D plotting library which produces
publication-quality figures in a variety of hardcopy formats
and interactive environments across ,
platforms. “matplotlib
Plot Anatomy & Workflow
c+ os
The basic steps to creating plots with matplotlib are:
PEON,
it
Prepare data Createplot Plot
Customize plot Save plot Show plot
import matplotlib.pyplot as plt
x= 1,2,3,4]
y = [10,20,25,30]
fig = plt.figure()
ax g.add_subplot (111)
-plot(x, y, color='lightblue', Linewidth=3) >> import matplotlib.pyplot as pltAxes
All plotting is done with respect to an Axes. In most cases, a
subplot will fit your needs. A subplot is an axes on a grid system.
fig.add_axes()
axl = fig.add_subplot(221) # row-col-num
3 fig.add_subplot (212)
plt.subplots (nrows=2,ncols=2)
Draw points with lines or markers connecting them
Draw unconnected points, scaled or colored
({1,2,3],[3,4,5]) Plot vertical rectangles (constant width)
-barh([0.5,1,2.5],[0,1,2])] Plot horiontal rectangles (constant height)
Draw a horizontal line across axes
‘Line(0.65) Draw a vertical line across axes
,color='blue!) Draw filled polygons
plt.subplots()
mshow(img
cmap='gist_earth',
interpolation='neare:
axes2[0].pcolor (data2) Pseudocolor plot of 2D array
>> axes2[0].pcolormesh (data) Pseudocolor plot of 2D array
CS = plt.contour(Y,X,U) Plot contours
>>> axes2[2].contourf(datal) Plot filled contours
> clabel (CS) Label a contour plot
Vector Fields
axes[0,1].arrow(0,0,0.5,0.5) ‘Add an arrow to the axes
axes[1,1].quiver(y,z) Plot a 20 field of arrows
s[0,1].streamplot (X,Y,U,V) Plot 2D vector fields
PELEM MCL lcd}
1.hist(y) Plot a histogram
.boxplot(y) Make a box and whisker plot
>>> ax3.violinplot(z) Make a violin plot
Q customize Plot
Colors, Color Bars & Color Maps
> plt.plot(x, x, xX, x**2, x, x**3)
ax.plot(x, y, alpha = 0.4)
> ax.plot(x, y, c='k')
fig.colorbar(im, orientation='horizontal')
> im = ax. imshow(img,
cmap='seismic')
UT ie)
> fig, ax = plt.subplots()
ax.scatter(x,y,marker=".")
> ax.plot(x,y,marker="0")
ncesy syBoe P Ula t)
>>> ax.text(1,
-2.1,
>
style= )
>>> ax.annotate( ;
xy=(8, 0),
xycoords= 5
xytext=(10.5, 0),
textcoords= 5
arrowprops=dict (arrowstyle=
connectionstyle= ),)
DS tg
>>> plt.title(r
STi re Tie MeN ol
Limits & Autoscaling
>>> ax.margins(x=0.@,y=0.1) Add padding to a plot
>>> ax.axis( )d Set the aspect ratio of the plat to|
ax.set(xLim=[0,10.5],ylim=[-1.5,1.5]) Sat limits for x-and y-axis
>>> ax.set_xlim(9,10.5) Set limits for x-axis
Legends
>>> ax.set(title Seta title and x-and y-avis labels
ylabel=
xlabel=
>>> ax. legend( No overlapping plat elements
Ticks
>>> ax.xaxis.set (ticks=rang} Manually set x-ticks
ticklabels
>>> ax. tick_params(axis='y', Make y-ticks langer and goin and aut
direction=
length=10)
Subplot Spacing
>>>. fig3. subplots_adjust (wspace=0.5 Adjust the spacing between subplots
hspace
left=0.125,
right=0.9,
top=0.9,
bottom=@.1)
>>> fig.tight_layout() Fit subplot(s) in to the figure area
Axis Spines
>>> axl.spines|[ set_visible(Fal Make the top axis line for a plot invisible
>>> axl.spines[ ].set_position(( @) }} Move the bottom axis line outward
Save figures
>>> plt.savefig(
Save transparent figures
>>> plt.savefig( , transparent=tTrue)
Show Plot
Close & Clear
>>> plt.cla() Clear an axis
>>> plt.clf() Clear the entire figure
>>> plt.close() Clase a window
Learn python for Data Science Interactively ® | INSAID
Irena cA, OF aN TA SCE