I am trying to plot a bar chart and attach labels to each bar. I can plot the chart with this code:
y = np.array([ 0.06590843, 0.10032079, 0.03295421, 0.12277632, 0.04257801,
0.00641586, 0.05774278, 0.15106445, 0.13852435, 0.03732867,
0.05570137, 0.11548556, 0.22834646, 0.09477982, 0.12569262,
0.09711286, 0.05920093, 0.03295421, 0.11286089, 0.05453485,
0.08486439, 0.09857101, 0.00641586])
x= ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '20', '40', '60', '80', '100', '300', '500', '700', '900', '1000', '1300', '1512']
plt.figure(figsize = (16, 2))
plt.bar(range(23), y)
but when I try to add the labels with this:
plt.xticks(x)
I get the following error:
AttributeError: 'NoneType' object has no attribute 'seq'
If I do:
plt.bar(x, y)
The x labels get jumbled and I get a figure like this:
I am using %matplotlib inline for my backend.
I got it to work with this modification to plt.xticks(x)
plt.xticks(range(23), x)
The issue with your first code is that you are failing to specify the positions for the ticks. If you look up Matplotlib's documentation, you'll see that parameters for the xticks function are:
matplotlib.pyplot.xticks(ticks=None, labels=None, **kwargs)
ticks : array_like A list of positions at which ticks should be placed. You can pass an empty list to disable xticks.
labels : array_like, optional A list of explicit labels to place at the given locs.
**kwargs Text properties can be used to control the appearance of the labels.
(Source: https://matplotlib.org/api/_as_gen/matplotlib.pyplot.xticks.html)
Hence, in your original post you are incorrectly calling the function, as opposed to your second post where you give the ticks positions and labels.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With