Example 1:
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
#plt.xlabel('x_nums')
#可以选择在x轴写x轴的意义
#plt.title('Example1')
#写title
plt.show()
注意:plt.plot([1,2,3,4])
给如果给plot中只赋予一个list,则默认是y的数值。x轴是默认生成,维度与y的维度相同,从0开始。
Example2: plot show
import matplotlib.pyplot as plt
plt.plot([1,2,3,4], [1,4,9,16], 'ro')
plt.axis([0, 6, 0, 20])
plt.show()
plt.plot还有第三个参数,The default format string is ‘b-‘, which is a solid blue line。
可以修改为’ro’,为红色圆点
Example 3 : dif color
import numpy as np
import matplotlib.pyplot as plt# evenly sampled time at 200ms intervals
t = np.arange(0., 5., 0.2)# red dashes, blue squares and green triangles
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^') # y=x red -- ; y=x^2 blue squer ; y=x^3 green ∠
plt.show()
Working with multiple figures and axes
Example 4: Subfig
import numpy as np
import matplotlib.pyplot as pltdef f(t):return np.exp(-t) * np.cos(2*np.pi*t)t1 = np.arange(0.0, 5.0, 0.1)
t2 = np.arange(0.0, 5.0, 0.02)plt.figure(1) #第一张图
plt.subplot(211) #subplot(m,n,p) 将fig1分为m=2行,n=1列,从左到右,从上到下的第p=1张图
plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k') plt.subplot(212) #从左到右,从上到下的第p=2张图
plt.plot(t2, np.cos(2*np.pi*t2), 'r--')
plt.show()
#如下图所示,在同一张fig上有两个subfig。
Working with text
运用 text( ) 这个方法,可以在图像的任何位置添加文字
Example 5 :Histogram
import numpy as np
import matplotlib.pyplot as plt# Fixing random state for reproducibility(使每次生成的随机数都相同)
np.random.seed(19680801)mu, sigma = 100, 15
#从标准正态分布中返回10000个随机数
x = mu + sigma * np.random.randn(10000)# the histogram of the data
n, bins, patches = plt.hist(x, 50, normed=1, facecolor='g', alpha=0.75)plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title('Histogram of IQ')
#在(60,0.025)坐标处加入字符
plt.text(60, .025, r'$\mu=100,\ \sigma=15$')#x轴:(40,160) y轴:(0,0.03)
plt.axis([40, 160, 0, 0.03])
plt.grid(True)
plt.show()
关于np.random.seed()函数,可以参考
https://blog.csdn.net/IAMoldpan/article/details/78429165
Example 6 : Annotating text
annotate() 方法提供很方便的标注。在标注过程中,要考虑(x,y),以及xytext两个因素
import numpy as np
import matplotlib.pyplot as pltax = plt.subplot(111)t = np.arange(0.0, 5.0, 0.01)
s = np.cos(2*np.pi*t)
line, = plt.plot(t, s, lw=2)plt.annotate('local max', xy=(2, 1), xytext=(3, 1.5),arrowprops=dict(facecolor='black', shrink=0.05),)plt.ylim(-2,2)
plt.show()
Logarithmic and other nonlinear axes
matplot还能生成很多非线性的图,plt.xscale(‘log’) , plt.yscale(‘log’)
Example 7 :nonlinear axes
import numpy as np
import matplotlib.pyplot as pltfrom matplotlib.ticker import NullFormatter # useful for `logit` scale# Fixing random state for reproducibility
np.random.seed(19680801)# make up some data in the interval ]0, 1[
y = np.random.normal(loc=0.5, scale=0.4, size=1000)
y = y[(y > 0) & (y < 1)]
y.sort()
x = np.arange(len(y))# plot with various axes scales
plt.figure(1)# linear
plt.subplot(221)
plt.plot(x, y)
plt.yscale('linear')
plt.title('linear')
plt.grid(True)# log
plt.subplot(222)
plt.plot(x, y)
plt.yscale('log')
plt.title('log')
plt.grid(True)# symmetric log
plt.subplot(223)
plt.plot(x, y - y.mean())
plt.yscale('symlog', linthreshy=0.01)
plt.title('symlog')
plt.grid(True)# logit
plt.subplot(224)
plt.plot(x, y)
plt.yscale('logit')
plt.title('logit')
plt.grid(True)
# Format the minor tick labels of the y-axis into empty strings with
# `NullFormatter`, to avoid cumbering the axis with too many labels.
plt.gca().yaxis.set_minor_formatter(NullFormatter())
# Adjust the subplot layout, because the logit one may take more space
# than usual, due to y-tick labels like "1 - 10^{-3}"
plt.subplots_adjust(top=0.92, bottom=0.08, left=0.10, right=0.95, hspace=0.25,wspace=0.35)plt.show()
The end_