21

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

user9026
  • 852
  • 2
  • 9
  • 20

3 Answers3

31

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")
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
3

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

Robbie
  • 4,672
  • 1
  • 19
  • 24
1

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)
Community
  • 1
  • 1
kai06046
  • 292
  • 1
  • 3
  • 10