https://www.matplotlib.org.cn/tutorials/introductory/pyplot.html
1
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,2,100)#從0到2,分100步
plt.plot(x,x,label="linear")#曲線類型及取名
plt.plot(x,x**2,label="quadratic")
plt.plot(x,x**3,label="cubic")
plt.xlabel('x label')#坐標(biāo)軸的名字
plt.ylabel('y label')
plt.title("Simple Plot")
plt.legend() #沒有它label不顯示
plt.show()
2
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0,10,0.2)#從0到10,每步范圍為0.2
y = np.sin(x)
fig = plt.figure()#顯示一個(gè)板
ax = fig.add_subplot(111)#它能調(diào)整顯示的大小
ax.plot(x,y)#上三步等同于 plt.plot(x,y)
plt.show()
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0,10,0.4)#從0到10禽绪,每步范圍為0.2
plt.plot(x,x,'r--',x,x**2,'bs',x,x**3,'g^')#看圖吧怜奖,不明白
plt.show()
import matplotlib.pyplot as plt
import numpy as np
name = [1,2,3]
value = [1,10,100]
#設(shè)置畫布大小
plt.figure(1,figsize=(9,3))
#畫出3幅圖,分別設(shè)置
plt.subplot(131)
plt.bar(name,value)
plt.subplot(132)
plt.scatter(name,value)
plt.subplot(133)
plt.plot(name,value)
plt.suptitle("Categorical Plotting")
plt.show()
import matplotlib.pyplot as plt
import numpy as np
data = {'a' : np.arange(50),
'c': np.random.randint(0,50,50),
'd': np.random.randn(50)}
data['b'] = data['a']+10*np.random.randn(50)
data['d'] = np.abs(data['d'])*100
#x坐標(biāo) 數(shù)組a, y坐標(biāo) 數(shù)組b,顏色c 數(shù)組c,大小s 數(shù)組d
plt.scatter('a','b',c='c',s='d',data=data)
plt.xlabel('entry a')
plt.ylabel('entry b')
plt.show()
import matplotlib.pyplot as plt
plt.figure(1) # the first figure
plt.subplot(211) # the first subplot in the first figure
plt.plot([1, 2, 3])
plt.subplot(212) # the second subplot in the first figure
plt.plot([4, 5, 6])
plt.figure(2) # a second figure
plt.plot([4, 5, 6]) # creates a subplot(111) by default
plt.figure(1) # figure 1 current; subplot(212) still current
plt.subplot(211) # make subplot(211) in figure1 current
plt.title('Easy as 1, 2, 3') # subplot 211 title
plt.show()
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(19680801)
data = np.random.randn(2, 100)
fig, axs = plt.subplots(2, 2, figsize=(5, 5))
axs[0, 0].hist(data[0])
axs[1, 0].scatter(data[0], data[1])
axs[0, 1].plot(data[0], data[1])
axs[1, 1].hist2d(data[0], data[1])
plt.show()
import matplotlib.pyplot as plt
import numpy as np
mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)
# the histogram of the data
n, bins, patches = plt.hist(x, 50, normed=True, facecolor='g', alpha=0.75)
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title('Histogram of IQ')
plt.text(60, .025, r'$\mu=100,\ \sigma=15$') # 支持 LaTex格式
plt.axis([40, 160, 0, 0.03])
plt.grid(True)
plt.show()