This article shows some commonly used functions for drawing graphics in matplotlib.pyplot, and shows the use of pie charts, histograms, polar coordinates, and scatter plots. You can't learn all of them in this unit, and a function can be expanded. Many, this article aims to let beginners understand some common functions of matplotlib, more learning content can go to the official website to learn.
Table of contents
pyplot - an overview of basic chart functions
pyplot - drawing of pie charts
pyplot - drawing of histograms
pyplot - plotting polar coordinates
pyplot - drawing of scatter plots
pyplot - an overview of basic chart functions
Sixteen commonly used functions:
function | illustrate |
---|---|
plt.plot(x,y,fmt,…) | draw a coordinate graph |
plt.boxplot(data,notch,position) | draw a boxplot |
plt.bar(left,height,width,bottom) | draw a bar chart |
plt.barh(width,bottom,left,height) | draw a horizontal bar chart |
plt.polar(theta, r) | Plot polar coordinates |
plt.pie(data, explode) | draw a pie chart |
plt.psd(x,NFFT=256,pad_to,Fs) | Plot Power Spectral Density |
plt.specgram(x,NFFT=256,pad_to,F) | Plot the spectrum |
plt.cohere(x,y,NFFT=256,Fs) | Plot the X-Y correlation function |
plt.scatter(x,y) | Plot a scatter plot where x and y are the same length |
plt.step(x,y,where) | draw step diagram |
plt.hist(x,bins,normed) | draw a histogram |
plt.contour(X,Y,Z,N) | draw isoplots |
plt.vlines() | draw vertical graph |
plt.stem(x,y,linefmt,markerfmt) | draw firewood diagram |
plt.plot_date() | plot data date |
pyplot - drawing of pie charts
import matplotlib.pyplot as plt
sizes = [15,35,45,5]
explode = (0,0.1,0,0)
labels = 'Frogs','Hogs','Dogs','Logs'
plt.pie(sizes,explode=explode,labels=labels,autopct='%1.1f%%',
shadow=False,startangle=90)
plt.axis('equal')
plt.show()
image:
pyplot - drawing of histograms
pyplot - plotting polar coordinates
Drawing in an object-oriented way
import matplotlib.pyplot as plt
import numpy as np
N=20
theta = np.linspace(0.0,2*np.pi,N,endpoint=False)
radii = 10*np.random.rand(N)
width = np.pi/4*np.random.rand(N)
ax = plt.subplot(111,projection='polar')
bars =ax.bar(theta,radii,width=width,bottom=0.0)
for r,bar in zip(radii,bars):
bar.set_facecolor(plt.cm.viridis(r/10.))
bar.set_alpha(0.5)
plt.show()
image:
pyplot - drawing of scatter plots
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
ax.plot(10*np.random.randn(100),10*np.random.randn(100),'o')
ax.set_title('Simple Scatter')
plt.show()
image: