import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties # 修改字體 font=FontProperties(fname='C:\Windows\Fonts\simfang.ttf') plt.style.use('ggplot') # 設置條形圖的背景 classes=['3班','4班','5班','6班'] students = [66, 55, 45, 70] classes_index=range(len(classes)) plt.bar(classes_index,students,color='darkgreen') plt.xlabel('學生',fontproperties=font,fontsize=15) plt.ylabel('學生人數',fontproperties=font,fontsize=15) plt.title('班級-學生人數',fontproperties=font,fontsize=20) plt.xticks(classes_index,classes,fontproperties=font) plt.show()
import numpy as np import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties # 修改字體 font=FontProperties(fname='C:\Windows\Fonts\simhei.ttf') plt.style.use('ggplot') x1=np.random.randn(10000) # 隨機生成10000個小數 x2=np.random.randn(10000) fig = plt.figure() # 生成一張畫布 ax1 = fig.add_subplot(1,2,1) # 表示一行兩列取第一個 ax2 = fig.add_subplot(1,2,2) # 表示一行兩列取第二個 ax1.hist(x1,bins=50,color='green') # 表示10000個小數分到50個柱子上 ax2.hist(x2,bins=50,color='blue') ax1.set_title('x1的正太分佈',fontproperties=font) # 加子標題 ax2.set_title('x2的正太分佈',fontproperties=font) # 加子標題 fig.suptitle('兩個正太分佈',fontproperties=font,fontsize=20) # 父標題 plt.show()
import numpy as np import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties # 修改字體 font=FontProperties(fname='C:\Windows\Fonts\simhei.ttf') plt.style.use('ggplot') x1=np.random.randn(40).cumsum() # cumsum是講生成的隨機數一個個累加起來,做爲對應位置的新元素 x2=np.random.randn(40).cumsum() x3=np.random.randn(40).cumsum() x4=np.random.randn(40).cumsum() plt.plot(x1,color='r',linestyle='-',marker='o',label='紅圓線') plt.plot(x2,color='b',linestyle='--',marker='*',label='藍虛線') plt.plot(x3,color='black',linestyle='-.',marker='s',label='黑方線') plt.plot(x4,color='y',linestyle=':',marker='s',label='黃方線') plt.legend(loc='best',prop=font) # 顯示label,loc不指定默認在最佳位置顯示label,指定right就是將label在右邊顯示 plt.show()
# 生成一個對稱軸爲x=1的二次函數 import numpy as np import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties # 修改字體 font=FontProperties(fname='C:\Windows\Fonts\simhei.ttf') # plt.style.use('ggplot') x=np.arange(-20,21) # y = np.random.randn(20).cumsum() y=(x**2)+2*x+1 plt.scatter(x,y) plt.show()
import numpy as np import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties font=FontProperties(fname='C:\Windows\Fonts\simhei.ttf') plt.style.use('ggplot') x=np.arange(1,20,1) print(x) # 擬合一條水平散點線 np.random.seed(1) y_linear=x+10*np.random.randn(19) print(y_linear) # 擬合一條x**2 的散點線 y_quad=x**2+10*np.random.randn(19) print(y_quad) # s是散點大小 fig = plt.figure() ax1 = fig.add_subplot(121) plt.scatter(x,y_linear,s=30,color='r',label='藍點') plt.scatter(x,y_quad,s=100,color='b',label='紅點') ax2= fig.add_subplot(122) plt.plot(x,y_linear,color='r') plt.plot(x,y_quad,color='b') # 限制x軸和y軸的範圍取值 plt.xlim(min(x) -1,max(x) + 1) plt.ylim(min(y_quad) - 10,max(y_quad) + 10) fig.suptitle('散點圖+直線圖',fontproperties=font,fontsize=20) ax1.set_title('散點圖',fontproperties=font) ax1.legend(prop=font) # 最佳位置顯示label ax2.set_title('折線圖',fontproperties=font) plt.show()
import numpy as np import matplotlib.pyplot as plt from pylab import mpl from matplotlib.font_manager import FontProperties font=FontProperties(fname='C:\Windows\Fonts\simhei.ttf') mpl.rcParams['font.sans-serif'] = ['SimHei'] fig,ax = plt.subplots(subplot_kw=dict(aspect='equal')) recipe = ['優','良','輕度污染','中度污染','重度污染','嚴重污染','缺'] data = [2,49,21,9,11,6,2] colors = ['lime','yellow','darkorange','red','purple','maroon','grey',] wedges,texts,texts2 = ax.pie(data, wedgeprops=dict(width = 0.5), startangle=40, colors=colors, autopct='%1.0f%%', pctdistance=0.8) # print(wedges) ''' [<matplotlib.patches.Wedge object at 0x000000000EE46198>, <matplotlib.patches.Wedge object at 0x000000000EE46860>, <matplotlib.patches.Wedge object at 0x000000000EE46F28>, <matplotlib.patches.Wedge object at 0x000000000EE57630>, <matplotlib.patches.Wedge object at 0x000000000EE57CF8>, <matplotlib.patches.Wedge object at 0x000000000EE63400>, <matplotlib.patches.Wedge object at 0x000000000EE63AC8>] ''' # print(texts) # print(texts2) plt.setp(texts2,size=14,weight='bold') bbox_props=dict(boxstyle='squre',pad='0.3',fc='w',ec='k',lw=0.72) kw = dict(xycoords='data', textcoords='data', arrowprops=dict(arrowstyle='->'), bbox=None, zorder=0, va='center') for i ,p in enumerate(wedges): # print(i,p) ''' 0 Wedge(center=(0, 0), r=1, theta1=40, theta2=47.2, width=0.5) 1 Wedge(center=(0, 0), r=1, theta1=47.2, theta2=223.6, width=0.5) 2 Wedge(center=(0, 0), r=1, theta1=223.6, theta2=299.2, width=0.5) 3 Wedge(center=(0, 0), r=1, theta1=299.2, theta2=331.6, width=0.5) 4 Wedge(center=(0, 0), r=1, theta1=331.6, theta2=371.2, width=0.5) 5 Wedge(center=(0, 0), r=1, theta1=371.2, theta2=392.8, width=0.5) 6 Wedge(center=(0, 0), r=1, theta1=392.8, theta2=400, width=0.5) ''' ang=(p.theta2-p.theta1)/2. + p.theta1 y=np.sin(np.deg2rad(ang)) x = np.cos(np.deg2rad(ang)) horizontalalignment = {-1:'right',1:'left'}[int(np.sign(x))] connectionstyle='angle,angleA=0,angleB={}'.format(ang) kw['arrowprops'].update({'connectionstyle':connectionstyle}) ax.annotate(recipe[i], xy=(x,y), xytext=(1.25*np.sign(x),1.3*y), size=16, horizontalalignment=horizontalalignment, fontproperties=font, **kw) ax.set_title('餅圖示例',fontproperties=font) plt.show()
線型linestyle(-,-.,--,..)python
點型marker(v,^,s,*,H,+,x,D,o,....)dom
顏色color(b,g,r,y,k,w....)函數
設置圖像標題:plt.title()字體
設置x軸名稱:plt.xlabel()3d
設置y軸名稱:plt.ylabel()code
設置X軸範圍:plt.Xlim()orm
設置Y軸範圍:plt.ylim()blog
設置X軸刻度:plt.xtlcks()ip
設置Y軸刻度:plt.yticks()ci
設置曲線圖例:plt.legend()