So I’m trying to do a bar plot on a data where x
is the username (string ) and each x
is long enough to overlap each other, so I have to rotate the x
label. No problem there. However, when exporting the plot results, the x
label on the exported image is cropped. I tried using plt.tight_layout()
and worked, but it change the look of the plot. The code is similar to this
JavaScript
x
13
13
1
from matplotlib import pyplot as plt
2
3
x= ['abc', 'ronaldo', 'melon_killer_123456']
4
y= [1, 2, 3]
5
6
plt.bar(x, y)
7
8
plt.xticks(rotation = 90)
9
plt.savefig('a.png')
10
11
plt.show()
12
13
Exported image:
I want it to look like this (Got this by using jupyter notebook and manually save output image):
So how to do that?
Advertisement
Answer
You can play around with the rcParams size settings and the plt.subplots_adjust
settings until you get your desired image.
JavaScript
1
16
16
1
import matplotlib.pyplot as plt
2
x= ['abc', 'ronaldo', 'melon_killer_123456']
3
y= [1, 2, 3]
4
5
plt.rcParams["figure.figsize"] = (5,10)
6
plt.bar(x, y)
7
plt.xticks(rotation = 90)
8
plt.subplots_adjust(top=0.925,
9
bottom=0.20,
10
left=0.07,
11
right=0.90,
12
hspace=0.01,
13
wspace=0.01)
14
plt.savefig('a.png')
15
plt.show()
16