簡體   English   中英

使用for循環和python / matplotlib在圖中添加子圖

[英]Add a subplot within a figure using a for loop and python/matplotlib

我認為您不需要所有代碼來幫助我解決這個問題,這已經在3個月前開始工作了,但是現在子圖並沒有添加到同一圖中。 即,我希望表格中的4個國家/地區中的每個圖表都2比2填充不同的折線圖。 我目前得到的是一個帶有4個空圖表和4個后續單個圖表的圖形。 為什么現在不能正確添加到同一圖表?

從那時起,我已經升級了python和pandas以及我使用的anaconda(spyder)。 我目前正在使用以下版本:

Python 2.7.5 | Anaconda 1.8.0(64位)Matplotlib 1.3.1 Pandas 0.15.2

for i, group in df.groupby('Country'):
   chrt += 1 
   fig1.add_subplot(2,2, chrt)
   #fig1.subplots_adjust(hspace=1)
   group.plot(x='Month', y='Value (USD)',title= str(i))

我想到了幾件事。 確保fig1chrt非常初始化。 然后,您可以使用ax關鍵字指定要繪制在哪個軸上。

import matplotlib.pyplot as plt
fig1 = plt.figure()
chrt = 0
for i, group in df.groupby('Country'):
    chrt += 1 
    ax = fig1.add_subplot(2,2, chrt)
    group.plot(x='Month', y='Value (USD)',title=str(i), ax=ax)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM