Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib not showing first label on x axis for the Bar Plot

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 gettingenter image description here So how can this be rectified ? Matplotlib version is 2.0.0 and Numpy version is 1.12.1

Thanks

like image 599
user9026 Avatar asked Apr 28 '17 06:04

user9026


People also ask

How do I show all X labels in matplotlib?

To show all X coordinates (or Y coordinates), we can use xticks() method (or yticks()).

How do I add X-axis labels in matplotlib?

Use the xlabel() method in matplotlib to add a label to the plot's x-axis.

How do you label the X-axis in Python?

With Pyplot, you can use the xlabel() and ylabel() functions to set a label for the x- and y-axis.


3 Answers

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")
like image 59
ImportanceOfBeingErnest Avatar answered Oct 03 '22 05:10

ImportanceOfBeingErnest


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()

enter image description here

like image 34
Robbie Avatar answered Oct 03 '22 04:10

Robbie


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)
like image 41
kai06046 Avatar answered Oct 03 '22 05:10

kai06046