Been trying to show these subplots in matplotlib but the code only works at random.
Sometimes it shows the graphs while other times it just throws the blank space.
Can you please tell me where I am going wrong?
def min_avg_max_pol_city(num_cities = 5):
fig, ax = plt.subplots(nrows = 7 , ncols = 3 , figsize=(22,22))
plt.subplots_adjust(hspace = 1) #Set each sub plot proportional to the total length of the plot area
no2_min = ax[0,0]
sub_df = df[df['pollutant_id'] == 'NO2'].sort_values('pollutant_min', ascending = True).head(num_cities)
sns.barplot( ax = no2_min, x = sub_df.city, y = sub_df.pollutant_min)
no2_min.set_ylabel("Pollution Level", fontsize = 14)
no2_min.set_title("Cities with Least NO2 level", fontsize = 14)
no2_avg = ax[0,1]
sub_df = df[df['pollutant_id'] == 'NO2'].sort_values('pollutant_avg', ascending = False).head(num_cities)
sns.barplot( ax = no2_avg, x = sub_df.city, y = sub_df.pollutant_avg)
no2_avg.set_title('Cities with Average NO2 level', fontsize = 14)
plt.show()
no2_max = ax[0,2]
sub_df = df[df['pollutant_id'] == 'NO2'].sort_values('pollutant_max' , ascending = False).head(num_cities)
sns.barplot( ax = no2_max , x = sub_df.city, y = sub_df.pollutant_max)
no2_max.set_title('Cities with Max NO2 level' , fontsize = 14)
plt.show();
min_avg_max_pol_city(5)
This is a screenshot from jupyter notebook
CodePudding user response:
In Jupyter-Notebooks, plt.show() is usually not required. Try
return fig
at the end of your function min_avg_max_pol_city()
(instead of plt.show();
). If the return value of a notebook cell is a figure, Jupyter should render it. Remove the other plt.show()
lines as well.
CodePudding user response:
Try to call plt.show()
at the end of the code i.e., when you have plotted all subplots in the figure. For example, check how this example uses plt.show()
.