Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

I need to generate a whole bunch of vertically-stacked plots in matplotlib. The result will be saved using figsave and viewed on a webpage, so I don't care how tall the final image is as long as the subplots are spaced so they don't overlap.

No matter how big I allow the figure to be, the subplots always seem to overlap.

My code currently looks like

import matplotlib.pyplot as plt 

import my_other_module 

titles, x_lists, y_lists = my_other_module.get_data() 

fig = plt.figure(figsize=(10,60)) 

for i, y_list in enumerate(y_lists): 

plt.subplot(len(titles), 1, i) 

plt.xlabel("Some X label") 

plt.ylabel("Some Y label") 

plt.title(titles[i]) 

plt.plot(x_lists[i],y_list) 

fig.savefig('out.png', dpi=100)

1 Answer

0 votes
by (106k points)

You can use plt.tight_layout for that:-

import matplotlib.pyplot as plt 

fig, axes = plt.subplots(nrows=4, ncols=4) 

fig.tight_layout() 

plt.show()

Related questions

0 votes
1 answer
0 votes
1 answer
+2 votes
3 answers
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...