Python数据可视化——matplotlib使用
總第57篇
01|Figure和Subplot:
matplotlib的圖像都位于figure對象中,相當于一塊畫布。figure的屬性figsize是用來設置figure的大小的。subplot是用來存放坐標系的,一個figure中可以有多個subplot。
%matplotlib inline
import matplotlib.pyplot as plt
from numpy.random import randn
import numpy as np
fig=plt.figure()
ax1=fig.add_subplot(2,2,1)#表示在figure中建立2*2個坐標系,ax1位于第一個坐標中
ax2=fig.add_subplot(2,2,2)
ax3=fig.add_subplot(2,2,3)
在程序開頭加(%matplotlib)是為了顯示figure,如果不加則不會跳出figure圖框。而(%matplotlib inline)則是直接顯示在編程界面,不重新跳出做圖框。
如果我們沒有指定在哪個ax上進行作圖,matplotlib會默認選擇最后一個(如果沒有則創建一個)上進行繪制。下面這條命令就沒有指定。
plt.plot(randn(50).cumsum(),"k--")
ax1.hist(randn(100),bins=20,color='k',alpha=0.3)#在ax1上作圖
ax2.scatter(np.arange(30),np.arange(30)+3*randn(30))#在ax2上作圖
也可以直接一次性創建多個圖框,然后在使用的時候進行索引使用就行,比如下面的subplots(2,3)就是一次性建立兩行三列個坐標,而axes[0,1]則表示利用第0行第2列對應的圖框。
fig,axes=plt.subplots(2,3)
_=axes[0,1].hist(randn(100),bins=20,color='k',alpha=0.3)#加“_=”視為讓其不輸出randn產生的隨機數組
subplots的參數:除幾行幾列外,還有sharex和sharey,表示x(y)軸的刻度是否要保持相等的刻度。默認情況是False,即不相等。
調整subplot周圍的間距:默認情況下,matlibplot會在subplot外圍以及sbuplot之間留下一定的邊距。圖像的大小和間距是相關的,如果你調整了圖像大小,間距也會自動調整。利用Figure的subplots_adjust方法可以用來修改間距。
plt.subplots_adjust(left=None,right=None,top=None,bottom=None,wspace=None,hspace=None)#分別表示左右上下邊距,以及寬度和高度百分比。
02|顏色,標記和線型:
常用顏色用英文字母的首字母來代替。
b---blue?? c---cyan? g---green??? k----black
m---magenta ? r---red? ? w---white??? y----yellow
標記是用在線性圖上來強調實際數據點的。
.? Point marker
,? Pixel marker
o? Circle marker
v? Triangle down marker?
^? Triangle up marker?
<? Triangle left marker?
>? Triangle right marker?
1? Tripod down marker
2? Tripod up marker
3? Tripod left marker
4? Tripod right marker
s? Square marker
p? Pentagon marker
*? Star marker
h? Hexagon marker
H? Rotated hexagon D Diamond marker
d? Thin diamond marker
| Vertical line (vlinesymbol) marker
_? Horizontal line (hline symbol) marker
+? Plus marker
x? Cross (x) marker
線性是表示線的形狀。
-????? 實線
--???? 短線
-.???? 短點相間線
:???? 虛點線
plot(randn(30).cumsum(),color="k",linestyle="--",marker="o")
03|刻度、標簽和標題:
fig=plt.figure()
ax=fig.add_subplot(1,1,1)
ax.plot(randn(1000).cumsum())
ticks=ax.set_xticks([0,250,500,750,1000])#設置x軸的刻度,y軸把x換成y即可
lables=ax.set_xticklabels(["one","two","three","four","five"],rotation=30,fontsize="small")#設置x軸對應的標簽,y軸把x換成y即可
ax.set_title("my first matplotlib plot")#為坐標軸設置標題
04|圖例:
在添加subplot的時候傳入label參數,然后調用ax.legend()或plt.legend()即可。
fig=plt.figure()
ax=fig.add_subplot(1,1,1)
ax.plot(randn(1000).cumsum(),label="one")#創建label標簽
ax.plot(randn(1000).cumsum(),label="two")#創建label標簽
ax.plot(randn(1000).cumsum(),label="three")#創建label標簽
ax.legend(loc="best")#loc是用來說明圖例的放置位置
06|Pandas作圖:
matplotlib是一種比較低級的工具,要組裝一張圖表,需要用到它的各種組件才可以,包括圖表類型(線型圖、柱狀圖、盒形圖、散布圖、等值線圖等)、圖例、標題、刻度標簽以及其他注釋信息。這是因為制作一張完整的圖表都需要用到這些,但是matplotlib要實現這種功能需要很多行代碼,而pandas可能只需要幾行代碼就可以搞定。
線型圖:Series和DataFrame都有自己的plot方法,plot默認創建的是線形圖,Series.plot()和DataFrame.plot()。
柱狀圖:需要給plot方法傳入參數kind,其中kind="bar"表垂直柱狀圖、kind="barh"表水平柱狀圖。Series和DataFrame的索引將會被用作X(或Y)軸的刻度。柱狀圖中有個特例就是堆積柱狀圖,只需要給plot傳入參數stacked="True"即可。還可以利用s.value_counts().plot(kind="bar")來圖形化顯示Series中各值出現的頻率。
直方圖:是一種可以對值頻率離散化顯示的柱狀圖。通過調用Series.hist()方法即可創建。
密度圖:與直方圖相關的一種類型圖,是通過計算“可能會產生觀測數據的連續概率分布的估計”而產生的,通過給plot傳入參數kind="kde"即可。
散布圖:是觀測兩個一維數據序列之間關系的有效手段,使用pd.scatter_matrix()即可建立。
總結
以上是生活随笔為你收集整理的Python数据可视化——matplotlib使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 医保卡的余额怎么查询余额 如何查询医保卡
- 下一篇: 怎样查询医保卡余额 医保卡余额怎么查询