數據可視化的時候,常常需要將多個子圖放在同一個畫板上進行比較,python 的matplotlib包下的subplot可以幫助完成子功能。
part1
繪制如下子圖
1
2
3
4
5
6
7
8
9
10
11
|
import matplotlib.pyplot as plt plt.figure(figsize = ( 6 , 6 ), dpi = 80 ) plt.figure( 1 ) ax1 = plt.subplot( 221 ) plt.plot([ 1 , 2 , 3 , 4 ],[ 4 , 5 , 7 , 8 ], color = "r" ,linestyle = "--" ) ax2 = plt.subplot( 222 ) plt.plot([ 1 , 2 , 3 , 5 ],[ 2 , 3 , 5 , 7 ],color = "y" ,linestyle = "-" ) ax3 = plt.subplot( 223 ) plt.plot([ 1 , 2 , 3 , 4 ],[ 11 , 22 , 33 , 44 ],color = "g" ,linestyle = "-." ) ax4 = plt.subplot( 224 ) plt.plot([ 1 , 2 , 3 , 4 ],[ 11 , 22 , 33 , 44 ],color = "b" ,linestyle = ":" ) |
其中:
plt.figure(figsize=(6,6), dpi=80) figsize表示畫板的大小,dpi為圖形的分辨率
plt.plot(x,y)plot函數內可以傳入兩個數據,一個表示橫軸一個表示y軸
ax1 = plt.subplot(221) 221表示將畫板分成兩行兩列,取第一個區域,即左上角區域
-plt.figure(1)表示取第一塊畫板,一個畫板即一張圖,如果有多個畫板,運行完就會打開多張圖(多個窗口)
color為線的顏色
linestyle為線的形狀
part2
如果要繪制如下圖
1
2
3
4
5
6
7
8
9
|
import matplotlib.pyplot as plt plt.figure(figsize = ( 6 , 6 ), dpi = 80 ) plt.figure( 1 ) ax1 = plt.subplot( 221 ) plt.plot([ 1 , 2 , 3 , 4 ],[ 4 , 5 , 7 , 8 ], color = "r" ,linestyle = "--" ) ax2 = plt.subplot( 222 ) plt.plot([ 1 , 2 , 3 , 5 ],[ 2 , 3 , 5 , 7 ],color = "y" ,linestyle = "-" ) ax3 = plt.subplot( 212 ) plt.plot([ 1 , 2 , 3 , 4 ],[ 11 , 22 , 33 , 44 ],color = "g" ,linestyle = "-." ) |
第三幅圖的坐標寫成212即可,即把畫板分成兩行一列取第二行
part3
要畫成如下的樣子,根據part2是一個道理
1
2
3
4
5
6
7
8
9
10
|
import matplotlib.pyplot as plt plt.figure(figsize = ( 6 , 6 ), dpi = 80 ) plt.figure( 1 ) ax1 = plt.subplot( 221 ) plt.plot([ 1 , 2 , 3 , 4 ],[ 4 , 5 , 7 , 8 ], color = "r" ,linestyle = "--" ) ax2 = plt.subplot( 223 ) plt.plot([ 1 , 2 , 3 , 5 ],[ 2 , 3 , 5 , 7 ],color = "y" ,linestyle = "-" ) ax3 = plt.subplot( 122 ) plt.plot([ 1 , 2 , 3 , 4 ],[ 11 , 22 , 33 , 44 ],color = "g" ,linestyle = "-." ) |
以上就是python使用matplotlib:subplot繪制多個子圖的示例的詳細內容,更多關于python matplotlib:subplot繪圖的資料請關注服務器之家其它相關文章!
原文鏈接:https://blog.csdn.net/dpengwang/article/details/85058026