【Python】4500字、10個(gè)案例分享幾個(gè)Python可視化小技巧,助你繪制高質(zhì)量圖表
Python當(dāng)中,我們用于繪制圖表的模塊最基礎(chǔ)的可能就是matplotlib了,今天小編分享幾個(gè)用該模塊進(jìn)行可視化制作的技巧,幫助你繪制出更加高質(zhì)量的圖表。Python來(lái)制作可視化動(dòng)圖,讓你更加清楚的了解到數(shù)據(jù)的走勢(shì)數(shù)據(jù)集的導(dǎo)入
最開(kāi)始,我們先導(dǎo)入數(shù)據(jù)集,并且導(dǎo)入我們需要用到的庫(kù)
import?pandas?as?pd
import?matplotlib.pyplot?as?plt
plt.style.use("seaborn-darkgrid")
#?讀取數(shù)據(jù)
aapl?=?pd.read_csv("AAPL.csv")
print(aapl.head())
output
????????Date????????Open????????High??...???????Close???Adj?Close????Volume
0??2021-9-30??143.660004??144.380005??...??141.500000??141.293793??88934200
1??2021-10-1??141.899994??142.919998??...??142.649994??142.442108??94639600
2??2021-10-4??141.759995??142.210007??...??139.139999??138.937225??98322000
3??2021-10-5??139.490005??142.240005??...??141.110001??140.904358??80861100
4??2021-10-6??139.470001??142.149994??...??142.000000??141.793060??83221100
簡(jiǎn)單的折線圖
plt.figure(figsize=(12,6))
plt.plot(aapl["Close"])
output

添加標(biāo)題以及設(shè)置Y軸標(biāo)簽
第一步我們先給圖表添加標(biāo)題以及給X軸、Y軸設(shè)置標(biāo)簽,代碼如下
plt.figure(figsize=(12,6))
plt.plot(aapl["Close"])
#?添加標(biāo)題和給Y軸打上標(biāo)記
plt.ylabel("Closing?Price",?fontsize=15)??##?收盤(pán)價(jià)
plt.title("Apple?Stock?Price",?fontsize=18)?##?標(biāo)題:蘋(píng)果公司股價(jià)
output

再添加一個(gè)Y軸
現(xiàn)有的這個(gè)Y軸代表的是收盤(pán)價(jià),要是我們還想再往圖表當(dāng)中添加另外一列的數(shù)據(jù),該數(shù)據(jù)的數(shù)值范圍和已有的收盤(pán)價(jià)的數(shù)值范圍不同,如果放在一起,繪制出來(lái)的圖表可不好看,如下
plt.figure(figsize=(12,6))
plt.plot(aapl["Close"])
#?第二根折線圖
plt.plot(aapl["Volume"])
#?Y軸的名稱和標(biāo)記
plt.ylabel("Closing?Price",?fontsize=15)
plt.title("Apple?Stock?Price",?fontsize=18)
output

fig,?ax1?=?plt.subplots(figsize=(12,6))
#?第二個(gè)Y軸的標(biāo)記
ax2?=?ax1.twinx()
ax1.plot(aapl["Close"])
ax2.plot(aapl["Volume"],?color="r")
#?添加標(biāo)題和Y軸的名稱,有兩個(gè)Y軸
ax1.set_ylabel("Closing?Price",?fontsize=15)
ax2.set_ylabel("Volume",?fontsize=15)
plt.title("Apple?Stock?Price",?fontsize=18)
output

twinx()方法再來(lái)新建一個(gè)Y軸對(duì)象,然后對(duì)應(yīng)的數(shù)據(jù)是Volume這一列當(dāng)中的數(shù)據(jù),而給Y軸標(biāo)記的方式也從上面的plt.ylabel()變成了ax.set_ylabel()添加圖例
fig,?ax1?=?plt.subplots(figsize=(12,6))
#?第二個(gè)Y軸
ax2?=?ax1.twinx()
ax1.plot(aapl["Close"])
ax2.plot(aapl["Volume"],?color="r")
#?設(shè)置Y軸標(biāo)簽和標(biāo)題
ax1.set_ylabel("Closing?Price",?fontsize=15)
ax2.set_ylabel("Volume",?fontsize=15)
plt.title("Apple?Stock?Price",?fontsize=18)
#?添加圖例
ax1.legend(["Closing?price"],?loc=2,?fontsize=12)
ax2.legend(["Volume"],?loc=2,?bbox_to_anchor=(0,?0.9),?fontsize=12)
output

plt.legend()方法當(dāng)中的loc參數(shù)代表的是圖例的位置,2代表的是左上方,具體的大家可以通過(guò)下面的鏈接來(lái)查閱https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.legend.html
將網(wǎng)格線去除掉
有時(shí)候我們感覺(jué)圖表當(dāng)中的網(wǎng)格線有點(diǎn)礙眼,就可以將其去掉,代碼如下
fig,?ax1?=?plt.subplots(figsize=(12,6))
#?第二個(gè)Y軸
ax2?=?ax1.twinx()
ax1.plot(aapl["Close"])
ax2.plot(aapl["Volume"],?color="r")
#?設(shè)置Y軸標(biāo)簽和標(biāo)題
ax1.set_ylabel("Closing?Price",?fontsize=15)
ax2.set_ylabel("Volume",?fontsize=15)
plt.title("Apple?Stock?Price",?fontsize=18)
#?添加圖例
ax1.legend(["Closing?price"],?loc=2,?fontsize=12)
ax2.legend(["Volume"],?loc=2,?bbox_to_anchor=(0,?0.9),?fontsize=12)
#?去掉網(wǎng)格線
ax1.grid(False)
ax2.grid(False)
output

這樣出來(lái)的圖表是不是看著順眼多了呢?!
在圖表當(dāng)中添加一些文字
fig,?ax1?=?plt.subplots(figsize=(12,6))
#?第二個(gè)Y軸
ax2?=?ax1.twinx()
ax1.plot(aapl["Close"])
ax2.plot(aapl["Volume"],?color="r")
#?設(shè)置Y軸標(biāo)簽和標(biāo)題
ax1.set_ylabel("Closing?Price",?fontsize=15)
ax2.set_ylabel("Volume",?fontsize=15)
plt.title("Apple?Stock?Price",?fontsize=18)
#?添加圖例
ax1.legend(["Closing?price"],?loc=2,?fontsize=12)
ax2.legend(["Volume"],?loc=2,?bbox_to_anchor=(0,?0.9),?fontsize=12)
#?去掉網(wǎng)格線
ax1.grid(False)
ax2.grid(False)
date_string?=?datetime.strptime("2021-10-31",?"%Y-%m-%d")
#?添加文字
ax1.text(
????date_string,?##?代表的是添加的文字的位置
????170,?
????"Nice?plot!",?##?添加的文字的內(nèi)容
????fontsize=18,?##?文字的大小
????color="green"?##?顏色
)
output

圖表當(dāng)中的中文顯示
在上面的圖表當(dāng)中,無(wú)論是標(biāo)題還是注釋或者是圖例,都是英文的,我們需要往里面添加中文的內(nèi)容時(shí)候,還需要添加下面的代碼
plt.rcParams['font.sans-serif']?=?['SimHei']
fig,?ax1?=?plt.subplots(figsize=(12,6))
#?第二個(gè)Y軸
ax2?=?ax1.twinx()
ax1.plot(aapl["Close"])
ax2.plot(aapl["Volume"],?color="r")
#?設(shè)置Y軸標(biāo)簽和標(biāo)題
ax1.set_ylabel("收盤(pán)價(jià)",?fontsize=15)
ax2.set_ylabel("成交量",?fontsize=15)
plt.title("蘋(píng)果公司股價(jià)走勢(shì)",?fontsize=18)
#?添加圖例
ax1.legend(["Closing?price"],?loc=2,?fontsize=12)
ax2.legend(["Volume"],?loc=2,?bbox_to_anchor=(0,?0.9),?fontsize=12)
#?去掉網(wǎng)格線
ax1.grid(False)
ax2.grid(False)
#?添加文字
ax1.text(
????date_string,
????170,?
????"畫(huà)的漂亮",?
????fontsize=18,?
????color="green"
)
output

這樣全局的字體都被設(shè)置成了“黑體”,文本內(nèi)容都是用中文來(lái)顯示
X軸/Y軸上刻度字體的大小
plt.rcParams["axes.edgecolor"]?=?"black"
plt.rcParams["axes.linewidth"]?=?2
#?tick?size
ax1.tick_params(axis='both',?which='major',?labelsize=13)
ax2.tick_params(axis='both',?which='major',?labelsize=13)
output

出來(lái)的圖表是不是比一開(kāi)始的要好很多呢?
制作動(dòng)圖
Python庫(kù),bar_chart_race,只需要簡(jiǎn)單的幾行代碼,就可以制作出隨著時(shí)間變化的直方圖動(dòng)圖,代碼如下import?bar_chart_race?as?bcr
import?pandas?as?pd
#?生成GIF圖像
df?=?pd.read_csv('covid19_tutorial.csv',?index_col=index_col,
?????????????????parse_dates=parse_dates)
bcr.bar_chart_race(df,?'covid19_tutorial_horiz.gif')
output

大家若是感興趣,可以登上它的官網(wǎng)
https://www.dexplo.org/bar_chart_race/
來(lái)了解更多如何使用該模塊來(lái)制作Python可視化動(dòng)圖的案例
往期精彩回顧
評(píng)論
圖片
表情
