1.折線圖在繪制折線圖時,如果你的數據很小,圖表的線條有點折,當你數據集比較大時候,比如超過100個點,則會呈現相對平滑的曲線。 在這里,我們使用三個plt.plot繪制了,不同斜率(1,2和3)的三條線。 import numpy as np import matplotlib.pyplot as plt cc= np.linspace(0,2,100) plt.rcParams['font.sans-serif'] = ['SimHei'] plt.plot(cc,cc,label='linear') plt.plot(cc,cc**2,label='兩倍') plt.plot(cc,cc**3,label='三倍') plt.xlabel('x label') plt.ylabel('y label') plt.title('折線圖') plt.legend() plt.show() cc = np.linspace(0,2,100) plt.plot(cc,cc,label ='linear') plt.plot(cc,cc ** 2,label ='quadratic') plt.plot(cc,cc ** 3,label ='cubic') plt.xlabel('x label') plt.ylabel('y label') 結果顯示,如下: 注意為了顯示中文,我們plt.rcParams屬性設置了中文字體,不然不能正確顯示中文title的。 3.直方圖直方圖也是一種常用的簡單圖表,本例中我們在同一張圖片中繪制兩個概率直方圖。 import numpy as np import matplotlib.pyplot as plt np.random.seed(19680801) mu1, sigma1 = 100, 15 mu2, sigma2 = 80, 15 x1 = mu1 + sigma1 * np.random.randn(10000) x2 = mu2 + sigma2 * np.random.randn(10000) n1, bins1, patches1 = plt.hist(x1, 50, density=True, facecolor='g', alpha=1) n2, bins2, patches2 = plt.hist(x2, 50, density=True, facecolor='r', alpha=0.2) plt.rcParams['font.sans-serif'] = ['SimHei'] plt.xlabel('智商') plt.ylabel('置信度') plt.title('IQ直方圖') plt.text(110, .025, r'$mu=100, sigma=15$') plt.text(50, .025, r'$mu=80, sigma=15$') # 設置坐標范圍 plt.axis([40, 160, 0, 0.03]) plt.grid(True) plt.show() 顯示效果為: 4.條形圖我們要介紹的第四種,圖表類型是條形圖,我們這兒引入稍微比較復雜的條形圖。 4.1平行條形圖此例中,我們引入三組(a,b,c)5個隨機數(0~1),并用條形圖打印出來,做比較 import numpy as np import matplotlib.pyplot as plt size = 5 a = np.random.random(size) b = np.random.random(size) c = np.random.random(size) x = np.arange(size) total_width, n = 0.8, 3 width = total_width / n # redraw the coordinates of x x = x - (total_width - width) / 2 # here is the offset plt.bar(x, a, width=width, label='a') plt.bar(x + width, b, width=width, label='b') plt.bar(x + 2 * width, c, width=width, label='c') plt.legend() plt.show() 顯示效果為: 4.2堆積條形圖數據同上,不過條形plot的時候,用的相互的值大小差異(水平方向),而不是條柱平行對比。 import numpy as np import matplotlib.pyplot as plt size = 5 a = np.random.random(size) b = np.random.random(size) c = np.random.random(size) x = np.arange(size) plt.bar(x, a, width=0.5, label='a',fc='r') plt.bar(x, b, bottom=a, width=0.5, label='b', fc='g') plt.bar(x, c, bottom=a+b, width=0.5, label='c', fc='b') plt.ylim(0, 2.5) plt.legend() plt.grid(True) plt.show() 顯示效果為: 5.2嵌套餅圖import numpy as np import matplotlib.pyplot as plt size = 0.3 vals = np.array([[60., 32.], [37., 40.], [29., 10.]]) cmap = plt.get_cmap('tab20c') outer_colors = cmap(np.arange(3)*4) inner_colors = cmap(np.array([1, 2, 5, 6, 9, 10])) print(vals.sum(axis=1)) # [92. 77. 39.] plt.pie(vals.sum(axis=1), radius=1, colors=outer_colors, wedgeprops=dict(width=size, edgecolor='w')) print(vals.flatten()) # [60. 32. 37. 40. 29. 10.] plt.pie(vals.flatten(), radius=1-size, colors=inner_colors, wedgeprops=dict(width=size, edgecolor='w')) # equal makes it a perfect circle plt.axis('equal') plt.show() 顯示效果為: 5.3極軸餅圖極軸餅圖是一種非??岬膱D表,讓我們看他的源碼: import numpy as np import matplotlib.pyplot as plt np.random.seed(19680801) N = 10 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() 顯示效果為: 6.3D圖表3D圖表也是能我們展示出超想象力的視覺效果的圖表。 6.1三維散點圖首先來看看三維的散點圖,源碼: import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D data = np.random.randint(0, 255, size=[40, 40, 40]) x, y, z = data[0], data[1], data[2] ax = plt.subplot(111, projection='3d') ax.scatter(x[:10], y[:10], z[:10], c='y') ax.scatter(x[10:20], y[10:20], z[10:20], c='r') ax.scatter(x[30:40], y[30:40], z[30:40], c='g') ax.set_zlabel('Z') ax.set_ylabel('Y') ax.set_xlabel('X') plt.show() 顯示效果為:
怎么樣,效果很酷把,好今天就給大家介紹到這里,如果你有任何問題,可以加到我的一個交流?:548+377+875 源碼也有!教程也有!一起交流 快速入門! |
|
|