Here is the code I am working on to create a logarithmic bar plot
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize = (12,6))
ax = fig.add_subplot(111)
x = ['Blue Whale', 'Killer Whale', 'Bluefin tuna', \
'Bottlenose dolphin', "Maui's dolphin", 'Flounder',\
'Starfish', 'Spongebob Squarepants']
y = [190000, 5987, 684, 650, 40, 6.8, 5, 0.02]
ax.bar(np.arange(len(x)),y, log=1)
ax.set_xticklabels(x, rotation = 45)
fig.savefig(filename = "f:/plot.png")
Now this is creating the bar plot where its not showing the first label, which is Blue Whale
. Here is the plot I am getting So how can this be rectified ? Matplotlib version is 2.0.0
and Numpy version is 1.12.1
Thanks
To show all X coordinates (or Y coordinates), we can use xticks() method (or yticks()).
Use the xlabel() method in matplotlib to add a label to the plot's x-axis.
With Pyplot, you can use the xlabel() and ylabel() functions to set a label for the x- and y-axis.
In matplotlib 2.0 there might be unshown tickmarks at the edges of the axes. To be on the safe side, you can set the tick locations in addition to the tick labels,
ax.set_xticks(np.arange(len(x)))
ax.set_xticklabels(x, rotation = 45)
You may also want to set the labels to align to their right edge, if they're rotated:
ax.set_xticklabels(x, rotation = 45, ha="right")
Yeah agreed that's kinda weird. Anyway, here's a way around it (just define the xticks before).
import matplotlib.pyplot as plt
import numpy as np
x = ['Blue Whale', 'Killer Whale', 'Bluefin tuna', \
'Bottlenose dolphin', "Maui's dolphin", 'Flounder',\
'Starfish', 'Spongebob Squarepants']
y = [190000, 5987, 684, 650, 40, 6.8, 5, 0.02]
fig = plt.figure(figsize = (12,6))
ax = fig.add_subplot(111)
ax.bar(np.arange(len(x)),y, log=1)
ax.set_xticks(np.arange(len(x)))
ax.set_xticklabels(x, rotation = 45, zorder=100)
fig.show()
set_xticklabels() will set the displayed text refer to this. So modify like this should work:
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize = (12,6))
ax = fig.add_subplot(111)
x = ['Blue Whale', 'Killer Whale', 'Bluefin tuna', \
'Bottlenose dolphin', "Maui's dolphin", 'Flounder',\
'Starfish', 'Spongebob Squarepants']
y = [190000, 5987, 684, 650, 40, 6.8, 5, 0.02]
pos = np.arange(len(x))
ax.bar(pos,y, log=1)
ax.set_xticks(pos)
ax.set_xticklabels(x, rotation = 45)
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