I am creating subplots of 3X5 but the plots are narrow and tall. 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.
If the width is too narrow, you should expand the graph area. You can also use MonthLocator()
and DateFormatter()
. Next, the interval between the graphs is controlled by subplots_adjust()
. Finally, label_outer()
is used to adjust the display of outer x,y axis.
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.