簡體   English   中英

如何使用 matplotblib 調整每個子圖的大小

[英]how to adjust each subplot sizes using matplotblib

我正在創建 3X5 的子圖,但這些圖又窄又高。 我想讓地塊更寬更短。

如何調整子圖大小?

這是代碼

fig, ax = plt.subplots(3,5, figsize=(15,15))
counter = 0
for i in range(3):
    for j in range(5):
        ax[i][j].plot(bars_pivot_df['date'],bars_pivot_df[unique_metro_regions[counter]], c ='red', label = 'DMA')
        ax[i][j].plot(bars_pivot_df['date'],bars_pivot_df['Entire Geography'], c ='blue', label = 'statewide')
        ax[i][j].set_title(unique_metro_regions[counter]) 
        l = ax[i][j].fill_between(bars_pivot_df['date'], bars_pivot_df[unique_metro_regions[counter]])
#         plt.subplots_adjust(left=0.1, right=0.9, bottom=0.1, top=0.9)
        counter = counter + 1
plt.show()

我嘗試使用 subplots_adjust 方法,但我不確定它是如何工作的。

這就是我當前的 plot 的樣子 - 在此處輸入圖像描述

如果寬度太窄,則應擴大圖形區域。 您還可以使用MonthLocator()DateFormatter() 接下來,圖表之間的間隔由subplots_adjust()控制。 最后, label_outer()用於調整外部 x,y 軸的顯示。

import pandas as pd
import numpy as np
import random
date_rng = pd.date_range('2018-01-01','2019-12-31', freq='1D')
val = np.random.randint(0,500,(730,))
country = ['country_'+str(x) for x in range(15)]
df = pd.DataFrame({'date':pd.to_datetime(date_rng), 'value':val})
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

fig, ax = plt.subplots(3,5, figsize=(20,15))
fig.subplots_adjust(wspace=0.3, hspace=0.1)

counter = 0
for i in range(3):
    for j in range(5):
        ax[i][j].plot(df['date'], df['value'], c ='blue')
        ax[i][j].set_title(country[counter]) 
        l = ax[i][j].fill_between(df['date'], df['value'])

        ax[i][j].xaxis.set_major_locator(mdates.MonthLocator(bymonth=None, interval=6, tz=None))
        ax[i][j].xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m-%d"))
        ax[i][j].label_outer()
        ax[i][j].tick_params(axis='x', labelrotation=45)
        counter = counter + 1

plt.show()

在此處輸入圖像描述

暫無
暫無

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

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