發(fā)布日期:2022-02-04 18:20 | 文章來源:gibhub
python通過Matplotlib繪制常見的幾種圖形
一、使用matplotlib對(duì)幾種常見的圖形進(jìn)行繪制
import numpy as np import matplotlib.pyplot as plt %matplotlib inline #寫了這個(gè)就可以不用寫plt.show() plt.rcParams['font.sans-serif']=['SimHei'] #用來正常顯示中文標(biāo)簽 plt.rcParams['axes.unicode_minus']=False #用來正常顯示負(fù)號(hào) X = np.linspace(0, 2*np.pi,100)# 均勻的劃分?jǐn)?shù)據(jù) Y = np.sin(X) Y1 = np.cos(X) plt.title("Hello World!!") plt.plot(X,Y) plt.plot(X,Y1)
X = np.linspace(0, 2*np.pi,100) Y = np.sin(X) Y1 = np.cos(X) plt.subplot(211) # 等價(jià)于 subplot(2,1,1) #一個(gè)圖版畫兩個(gè)圖 plt.plot(X,Y) plt.subplot(212) plt.plot(X,Y1,color = 'r')
1、柱狀圖
data = [5,25,50,20] plt.bar(range(len(data)),data)
2、水平繪制柱狀圖
data = [5,25,50,20] plt.barh(range(len(data)),data)
3、多個(gè)柱狀圖
data = [[5,25,50,20], [4,23,51,17], [6,22,52,19]] X = np.arange(4) plt.bar(X + 0.00, data[0], color = 'b', width = 0.25,label = "A") plt.bar(X + 0.25, data[1], color = 'g', width = 0.25,label = "B") plt.bar(X + 0.50, data[2], color = 'r', width = 0.25,label = "C") # 顯示上面設(shè)置的 lable plt.legend()
4、疊加型柱狀圖
data = [[5,25,50,20], [4,23,51,17], [6,22,52,19]] X = np.arange(4) plt.bar(X, data[0], color = 'b', width = 0.25) plt.bar(X, data[1], color = 'g', width = 0.25,bottom = data[0]) plt.bar(X, data[2], color = 'r', width = 0.25,bottom = np.array(data[0]) + np.array(data[1])) plt.show()
5、散點(diǎn)圖
N = 50 x = np.random.rand(N) y = np.random.rand(N) plt.scatter(x, y)
6、氣泡圖
N = 50 x = np.random.rand(N) y = np.random.rand(N) colors = np.random.randn(N) # 顏色可以用數(shù)值表示 area = np.pi * (15 * np.random.rand(N))**2 # 調(diào)整大小 plt.scatter(x, y, c=colors, alpha=0.5, s = area)
N = 50 x = np.random.rand(N) y = np.random.rand(N) colors = np.random.randint(0,2,size =50) plt.scatter(x, y, c=colors, alpha=0.5,s = area)
7、直方圖
a = np.random.rand(100) plt.hist(a,bins= 20) plt.ylim(0,15)
a = np.random.randn(10000) plt.hist(a,bins=50) plt.title("標(biāo)準(zhǔn)正太分布")
8、箱線圖
x = np.random.randint(20,100,size = (30,3)) plt.boxplot(x) plt.ylim(0,120) # 在x軸的什么位置填一個(gè) label,我們這里制定在 1,2,3 位置,寫上 A,B,C plt.xticks([1,2,3],['A','B','C']) plt.hlines(y = np.median(x,axis = 0)[0] ,xmin =0,xmax=3)
二、添加文字描述
1、文字描述一
# 設(shè)置畫布顏色為 blue fig, ax = plt.subplots(facecolor='blue') # y 軸數(shù)據(jù) data = [[5,25,50,20], [4,23,51,17], [6,22,52,19]] X = np.arange(4) plt.bar(X+0.00, data[0], color = 'darkorange', width = 0.25,label = 'A') plt.bar(X+0.25, data[1], color = 'steelblue', width = 0.25,label="B") plt.bar(X+0.50, data[2], color = 'violet', width = 0.25,label = 'C') ax.set_title("Figure 2") plt.legend() # 添加文字描述 方法一 W = [0.00,0.25,0.50] for i in range(3): for a,b in zip(X+W[i],data[i]): plt.text(a,b,"%.0f"% b,ha="center",va= "bottom") plt.xlabel("Group") plt.ylabel("Num") plt.text(0.0,48,"TEXT")
2、文字描述二
X = np.linspace(0, 2*np.pi,100)# 均勻的劃分?jǐn)?shù)據(jù) Y = np.sin(X) Y1 = np.cos(X) plt.plot(X,Y) plt.plot(X,Y1) plt.annotate('Points', xy=(1, np.sin(1)), xytext=(2, 0.5), fontsize=16, arrowprops=dict(arrowstyle="->")) plt.title("這是一副測(cè)試圖!")
三、多個(gè)圖形描繪 subplots
%pylab inline pylab.rcParams['figure.figsize'] = (10, 6) # 調(diào)整圖片大小 # np.random.seed(19680801) n_bins = 10 x = np.random.randn(1000, 3) fig, axes = plt.subplots(nrows=2, ncols=2) ax0, ax1, ax2, ax3 = axes.flatten() colors = ['red', 'tan', 'lime'] ax0.hist(x, n_bins, normed=1, histtype='bar', color=colors, label=colors) ax0.legend(prop={'size': 10}) ax0.set_title('bars with legend') ax1.hist(x, n_bins, normed=1, histtype='bar', stacked=True) ax1.set_title('stacked bar') ax2.hist(x, n_bins, histtype='step', stacked=True, fill=False) ax2.set_title('stack step (unfilled)') # Make a multiple-histogram of data-sets with different length. x_multi = [np.random.randn(n) for n in [10000, 5000, 2000]] ax3.hist(x_multi, n_bins, histtype='bar') ax3.set_title('different sample sizes')
四、使用Pandas 繪圖
1、散點(diǎn)圖
import pandas as pd df = pd.DataFrame(np.random.rand(50, 2), columns=['a', 'b']) # 散點(diǎn)圖 df.plot.scatter(x='a', y='b')
2、繪制柱狀圖
df = pd.DataFrame(np.random.rand(10,4),columns=['a','b','c','d']) # 繪制柱狀圖 df.plot.bar()
3、堆積的柱狀圖
# 堆積的柱狀圖 df.plot.bar(stacked=True)
4、水平的柱狀圖
# 水平的柱狀圖 df.plot.barh(stacked=True)
5、直方圖
df = pd.DataFrame({'a':np.random.randn(1000)+1,'b':np.random.randn(1000),'c':np.random.randn(1000) - 1}, columns=['a', 'b', 'c']) # 直方圖 df.plot.hist(bins=20)
6、箱線圖
# 箱線圖 df = pd.DataFrame(np.random.rand(10, 5), columns=['A', 'B', 'C', 'D', 'E']) df.plot.box()
到此這篇關(guān)于python通過Matplotlib繪制常見的幾種圖形(推薦)的文章就介紹到這了,更多相關(guān)python Matplotlib內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
相關(guān)文章