我正在使用 matplotlib 創建兩個圖,每個圖都是在同一軸上顯示兩個指標的子圖。
我正在嘗試運行它們,以便它們顯示為兩個圖表但在一個圖形中,這樣當我保存圖形時,我會看到兩個圖。目前,運行第二個情節會覆寫記憶體中的第一個情節,所以我只能保存第二個情節。
我怎樣才能將它們繪制在一起?
我的代碼如下。
plot1 = plt.figure()
fig,ax1 = plt.subplots()
ax1.plot(dfSat['time'],dfSat['wind_at_altitude'], 'b-', label = "speed", linewidth = 5.0)
plt.title('Wind Speeds - Saturday - {}'.format(windloc))
plt.xlabel('Time of day')
plt.ylabel('Wind speed (mph)')
ax1.plot(dfSat['time'],dfSat['gust_at_altitude'], 'r-', label = "gust", linewidth = 5.0)
plt.legend(loc="upper right")
ax1.text(0.05, 0.95, calcmeassat, transform=ax1.transAxes, fontsize=30,
verticalalignment='top')
plt.ylim((0,100))
plot2 = plt.figure()
fig,ax2 = plt.subplots()
ax2.plot(dfSun['time'],dfSun['wind_at_altitude'], 'b-', label = "speed", linewidth = 5.0)
plt.title('Wind Speeds - Sunday - {}'.format(windloc))
plt.xlabel('Time of day')
plt.ylabel('Wind speed (mph)')
ax2.plot(dfSun['time'],dfSun['gust_at_altitude'], 'r-', label = "gust", linewidth = 5.0)
plt.legend(loc="upper right")
ax2.text(0.05, 0.95, calcmeassun, transform=ax2.transAxes, fontsize=30,
verticalalignment='top')
plt.ylim((0,100))
uj5u.com熱心網友回復:
- 如前所述,在您的情況下,您只需要一級子圖,例如
nrows=1, ncols=2. - 然而,在 matplotlib 3.4 中,有一個叫做 subfigures 的“subplotting subplots”
# create 1x2 subplots fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(16, 4)) # plot saturdays on the left dfSat.plot(ax=ax1, x='date', y='temp_min') dfSat.plot(ax=ax1, x='date', y='temp_max') ax1.set_ylim(-20, 50) ax1.set_title('Saturdays') # plot sundays on the right dfSun.plot(ax=ax2, x='date', y='temp_min') dfSun.plot(ax=ax2, x='date', y='temp_max') ax2.set_ylim(-20, 50) ax2.set_title('Sundays')
子圖
假設你想要一些更復雜的東西,比如讓左側顯示 2012 有自己的
suptitle,右側顯示 2015 有自己的suptitle。創建 1x2 子圖(左
subfig_l和右subfig_r),左側(頂部ax_lt和底部ax_lb)有 2x1 子圖,右側(頂部ax_rt和底部)有 2x1 子圖ax_rb:
# create 1x2 subfigures fig = plt.figure(constrained_layout=True, figsize=(12, 5)) (subfig_l, subfig_r) = fig.subfigures(nrows=1, ncols=2, wspace=0.07) # create top/box axes in left subfig (ax_lt, ax_lb) = subfig_l.subplots(nrows=2, ncols=1) # plot 2012 saturdays on left-top axes dfSat12 = dfSat.loc[dfSat['date'].dt.year.eq(2012)] dfSat12.plot(ax=ax_lt, x='date', y='temp_min') dfSat12.plot(ax=ax_lt, x='date', y='temp_max') ax_lt.set_ylim(-20, 50) ax_lt.set_ylabel('Saturdays') # plot 2012 sundays on left-top axes dfSun12 = dfSun.loc[dfSun['date'].dt.year.eq(2012)] dfSun12.plot(ax=ax_lb, x='date', y='temp_min') dfSun12.plot(ax=ax_lb, x='date', y='temp_max') ax_lb.set_ylim(-20, 50) ax_lb.set_ylabel('Sundays') # set suptitle for left subfig subfig_l.suptitle('2012', size='x-large', weight='bold') # create top/box axes in right subfig (ax_rt, ax_rb) = subfig_r.subplots(nrows=2, ncols=1) # plot 2015 saturdays on left-top axes dfSat15 = dfSat.loc[dfSat['date'].dt.year.eq(2015)] dfSat15.plot(ax=ax_rt, x='date', y='temp_min') dfSat15.plot(ax=ax_rt, x='date', y='temp_max') ax_rt.set_ylim(-20, 50) ax_rt.set_ylabel('Saturdays') # plot 2015 sundays on left-top axes dfSun15 = dfSun.loc[dfSun['date'].dt.year.eq(2015)] dfSun15.plot(ax=ax_rb, x='date', y='temp_min') dfSun15.plot(ax=ax_rb, x='date', y='temp_max') ax_rb.set_ylim(-20, 50) ax_rb.set_ylabel('Sundays') # set suptitle for right subfig subfig_r.suptitle('2015', size='x-large', weight='bold')
供參考的樣本資料:
import pandas as pd from vega_datasets import data df = data.seattle_weather() df['date'] = pd.to_datetime(df['date']) dfSat = df.loc[df['date'].dt.weekday.eq(5)] dfSun = df.loc[df['date'].dt.weekday.eq(6)]uj5u.com熱心網友回復:
它不是那樣作業的。子圖就是他們所謂的;主情節內的情節。
這意味著如果您需要兩個子圖;那么你需要一個包含兩個子圖的圖。
# figure object NOT plot object # useful when you want only one plot NO subplots fig = plt.figure() # 2 subplots inside 1 plot # 1 row, 2 columns fig, [ax1, ax2] = plt.subplots(1, 2) # then call plotting method on each axis object to # create plot on that subplot sns.histplot(...., ax=ax1) sns.violinplot(..., ax=ax2) # or using matplotlib like this ax1.plot() ax2.plot()了解有關子圖的更多資訊
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/438497.html標籤:Python matplotlib 子情节
