您好,登錄后才能下訂單哦!
http://matplotlib.org
示例代碼:
# 引入matplotlib包
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline #在jupyter notebook 里需要使用這一句命令
# 創(chuàng)建figure對象
fig = plt.figure()
運(yùn)行結(jié)果:會彈出一個figure窗口,如下圖所示
示例代碼:
# 指定切分區(qū)域的位置
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
ax3 = fig.add_subplot(2,2,3)
ax4 = fig.add_subplot(2,2,4)
# 在subplot上作圖
random_arr = np.random.randn(100)
#print random_arr
# 默認(rèn)是在最后一次使用subplot的位置上作圖,但是在jupyter notebook 里可能顯示有誤
plt.plot(random_arr)
# 可以指定在某個或多個subplot位置上作圖
# ax1 = fig.plot(random_arr)
# ax2 = fig.plot(random_arr)
# ax3 = fig.plot(random_arr)
# 顯示繪圖結(jié)果
plt.show()
運(yùn)行結(jié)果:僅右下角有圖
示例代碼:
import matplotlib.pyplot as plt
import numpy as np
plt.hist(np.random.randn(100), bins=10, color='b', alpha=0.3)
plt.show()
示例代碼:
import matplotlib.pyplot as plt
import numpy as np
# 繪制散點(diǎn)圖
x = np.arange(50)
y = x + 5 * np.random.rand(50)
plt.scatter(x, y)
plt.show()
示例代碼:
import matplotlib.pyplot as plt
import numpy as np
# 柱狀圖
x = np.arange(5)
y1, y2 = np.random.randint(1, 25, size=(2, 5))
width = 0.25
ax = plt.subplot(1,1,1)
ax.bar(x, y1, width, color='r')
ax.bar(x+width, y2, width, color='g')
ax.set_xticks(x+width)
ax.set_xticklabels(['a', 'b', 'c', 'd', 'e'])
plt.show()
示例代碼:
import matplotlib.pyplot as plt
import numpy as np
# 矩陣?yán)L圖
m = np.random.rand(10,10)
print(m)
plt.imshow(m, interpolation='nearest', cmap=plt.cm.ocean)
plt.colorbar()
plt.show()
示例代碼:
import matplotlib.pyplot as plt
import numpy as np
fig, subplot_arr = plt.subplots(2,2)
# bins 為顯示個數(shù),一般小于等于數(shù)值個數(shù)
subplot_arr[1,0].hist(np.random.randn(100), bins=10, color='b', alpha=0.3)
plt.show()
運(yùn)行結(jié)果:左下角繪圖
等價(jià)于ax.plot(x, y, linestyle=‘--’, color=‘r’)
示例代碼:
import matplotlib.pyplot as plt
import numpy as np
fig, axes = plt.subplots(2)
axes[0].plot(np.random.randint(0, 100, 50), 'ro--')
# 等價(jià)
axes[1].plot(np.random.randint(0, 100, 50), color='r', linestyle='dashed', marker='o')
設(shè)置刻度范圍
plt.xlim(), plt.ylim()
ax.set_xlim(), ax.set_ylim()
設(shè)置顯示的刻度
plt.xticks(), plt.yticks()
ax.set_xticks(), ax.set_yticks()
設(shè)置刻度標(biāo)簽
ax.set_xticklabels(), ax.set_yticklabels()
設(shè)置坐標(biāo)軸標(biāo)簽
ax.set_xlabel(), ax.set_ylabel()
設(shè)置標(biāo)題
ax.set_title()
ax.plot(label=‘legend’)
ax.legend(), plt.legend()
loc=‘best’:自動選擇放置圖例最佳位置
示例代碼:
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(1)
ax.plot(np.random.randn(1000).cumsum(), label='line0')
# 設(shè)置刻度
#plt.xlim([0,500])
ax.set_xlim([0, 800])
# 設(shè)置顯示的刻度
#plt.xticks([0,500])
ax.set_xticks(range(0,500,100))
# 設(shè)置刻度標(biāo)簽
ax.set_yticklabels(['Jan', 'Feb', 'Mar'])
# 設(shè)置坐標(biāo)軸標(biāo)簽
ax.set_xlabel('Number')
ax.set_ylabel('Month')
# 設(shè)置標(biāo)題
ax.set_title('Example')
# 圖例
ax.plot(np.random.randn(1000).cumsum(), label='line1')
ax.plot(np.random.randn(1000).cumsum(), label='line2')
ax.legend()
ax.legend(loc='best')
#plt.legend()
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。