熱線電話:13121318867

登錄
首頁大數據時代最詳細的matplotlib繪圖教程就在這里了!
最詳細的matplotlib繪圖教程就在這里了!
2020-08-07
收藏

matplotlib是我們經常會用到的一款python繪圖庫,操作簡單,幾行代碼就能很輕松地畫一些或簡單或復雜地圖形,線圖、直方圖、功率譜、條形圖、錯誤圖、散點圖以及費笛卡爾坐標圖等都不在話下。今天小編就具體給大家介紹一下matplotlib繪圖教程。

一、首先來了解一下matplotlib

1.matplotlib是基于python語言的開源數據繪圖包。matplotlib的對象體系嚴謹而有趣,為我們提供了巨大的發揮空間。在熟悉了核心對象之后,我們可以輕易的定制圖像。matplotlib使用numpy進行數組運算,并調用一系列其他的python庫來實現硬件交互。

2.matplotlib安裝

pip install matplotlib

3.Matplotlib導入

import matplotlib.pyplot as plt#為方便簡介為plt

import numpy as np#畫圖過程中會使用numpy

import pandas as pd#畫圖過程中會使用pandas

二、matplotlib繪圖

1.柱形圖,散點圖,餅狀圖,折線圖


import numpy as np
import pandas as pd
from pandas import Series, DataFrame
import matplotlib.pyplot as plt
%matplotlib inline

fig = plt.figure(figsize=(10,8))  #建立一個大小為10*8的畫板
ax1 = fig.add_subplot(331)  #在畫板上添加3*3個畫布,位置是第1個
ax2 = fig.add_subplot(3,3,2)
ax3 = fig.add_subplot(3,3,3)
ax4 = fig.add_subplot(334)
ax5 = fig.add_subplot(3,3,5)
ax6 = fig.add_subplot(3,3,6)
ax7 = fig.add_subplot(3,3,7)
ax8 = fig.add_subplot(3,3,8)
ax9 = fig.add_subplot(3,3,9)

ax1.plot(np.random.randn(10))
_ = ax2.scatter(np.random.randn(10),np.arange(10),color='r')  #作散點圖
ax3.hist(np.random.randn(20),bins=10,alpha=0.3)  #作柱形圖
ax4.bar(np.arange(10),np.random.randn(10))  #做直方圖
ax5.pie(np.random.randint(1,15,5),explode=[0,0,0.2,0,0])  #作餅形圖

x = np.arange(10)
y = np.random.randn(10)
ax6.plot(x,y,color='green')
ax6.bar(x,y,color='k')

data = DataFrame(np.random.randn(1000,10),
                 columns=['one','two','three','four','five','six','seven','eight','nine','ten'])
data2 = DataFrame(np.random.randint(0,20,(10,2)),columns=['a','b'])
data.plot(x='one',y='two',kind='scatter',ax=ax7)  #針對DataFrame的一些作圖
data2.plot(x='a',y='b',kind='bar',ax=ax8,color='red',legend=False)
data2.plot(x='a',y='b',kind='barh',color='m',ax=ax9)
#plt.tight_layout() #避免出現疊影
#plt.show()




2.蠟燭圖


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.finance as mpf
from pandas import Series, DataFrame
from matplotlib.pylab import date2num
%matplotlib inline

plt.rcParams['figure.autolayout'] = True
plt.rcParams['figure.figsize'] = 25,6
plt.rcParams['grid.alpha'] = .4
plt.rcParams['axes.unicode_minus'] = False
plt.rcParams['font.sans-serif'] = ['SimHei']

fig, ax = plt.subplots(1,1,figsize=(12,5))

mpf.candlestick_ohlc(ax=ax,quotes=data2.values[::3],width=.002,colorup='red',colordown='green')
plt.xticks(data2.date[::25],data.date.map(lambda x:x[:5])[::25],rotation=0)
ax.twiny().plot(data3.Open)
plt.tight_layout();



3.熱圖


import numpy as np
import pandas as pd
from pandas import Series, DataFrame
import matplotlib.pyplot as plt
%matplotlib inline


df = DataFrame(np.random.randn(10,10))
fig = plt.figure(figsize=(12,5))
ax = fig.add_subplot(111)
axim = ax.imshow(df.values,interpolation='nearest')#cmap=plt.cm.gray_r, #cmap用來顯示顏色,可以另行設置
plt.colorbar(axim)
plt.show()


以上就是小編今天跟大家分享的matplotlib繪圖的一些方法啦,希望對與大家使用matplotlib有所幫助。

數據分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數據分析師資訊
更多

OK
客服在線
立即咨詢
日韩人妻系列无码专区视频,先锋高清无码,无码免费视欧非,国精产品一区一区三区无码
客服在線
立即咨詢