4

Why is my X-axis ticks showing Negative values when the Xaxis values range from 43990 - 44003.

import matplotlib.pyplot as plt
x=[44000, 44001, 44002, 44003, 43990, 43991, 43992, 43993, 43994, 43995, 43996, 43997, 43998, 43999]
y=[8, 5, 3, 1, 1, 3, 4, 10, 4, 11, 4, 10, 17, 19]
plt.bar(x,y)
plt.show()

I am seeing the following output. I was expecting x-axis to range from 43990 - 44003 why negative x-axis ticks ?

I have tried this on a couple of machines, all showing similar strange behaviour on recent versions of python and matplotlib (tried a few different versions)

Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 18:10:19) [GCC 7.2.0] on linux

Python 3.4.3 (default, Nov 17 2016, 01:08:31)

Strangely enough many trivial toy example x and y arrays are giving me expected figures.

For e.g the follwowing snippet shows the expected graph with correct x-axis tick labels

x=[20,30,90,70, 50, 60, 80, 70]
y=[3,2,5,10, 3, 9, 7, 6]
plt.bar(x,y)
plt.show()

correct result for above example

What obvious thing am i missing here ?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
jithu83
  • 539
  • 6
  • 11
  • do you want to preserve the order of the x values? – briancaffey Jan 25 '18 at 03:33
  • The other question answers the same thing, but it didn't turn up in my extensive search for an anwer. I believe the way this question is framed provides few value additions - 1 very simple/basic example , (2) calling out -ve values which really confused , which might help others to find out the answer. Would providing a link to the other question address your concern – jithu83 Jan 25 '18 at 19:13

3 Answers3

4

You need to disable the offset:

plt.ticklabel_format(useOffset=False)

Another option would be to input custom tick labels:

plt.bar(x, y, tick_label=x)
Evan Nowak
  • 895
  • 4
  • 8
  • Thanks for the answer ... it works. If I was using axes to plot (i.e like ax.bar(x,y)) do you know the equalent of your first solution ? – jithu83 Jan 25 '18 at 03:55
  • @jithu83 Yeah, its the same: `ax.ticklabel_format(useOffset=False)` – Evan Nowak Jan 25 '18 at 14:49
1

You are missing the "+4.4e4" in the bottom right-hand corner of your figure. For example the tick label "-4" should be read as: 44000 - 4 = 43996

myrtlecat
  • 2,156
  • 12
  • 16
1

Your graph has +4.4e4 at the bottom. If that is true then it appears to be correct. 4.4e4 +4 = 44004 and 44000 - 10 = 43990. So I confused by your question since plot scale seems correct.

Natsfan
  • 4,093
  • 3
  • 22
  • 29