简体   繁体   English

如何使用 matplotblib 调整每个子图的大小

[英]how to adjust each subplot sizes using matplotblib

I am creating subplots of 3X5 but the plots are narrow and tall.我正在创建 3X5 的子图,但这些图又窄又高。 I want to make the plots a little bid wider and shorter.我想让地块更宽更短。

How do I adjust the subplot sizes?如何调整子图大小?

Here is the code这是代码

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()

I tried using subplots_adjust method but I am not sure how it works.我尝试使用 subplots_adjust 方法,但我不确定它是如何工作的。

This is how my current plot looks like -这就是我当前的 plot 的样子 - 在此处输入图像描述

If the width is too narrow, you should expand the graph area.如果宽度太窄,则应扩大图形区域。 You can also use MonthLocator() and DateFormatter() .您还可以使用MonthLocator()DateFormatter() Next, the interval between the graphs is controlled by subplots_adjust() .接下来,图表之间的间隔由subplots_adjust()控制。 Finally, label_outer() is used to adjust the display of outer x,y axis.最后, 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