14

Whenever I plot, the X axis sorts automatically (for example, if i enter values 3, 2, 4, it will automatically sort the X axis from smaller to larger.

How can I do it so the axis remains with the order I input the values i.e 3, 2, 4

   import pylab as pl
   data = genfromtxt('myfile.dat')
   pl.axis('auto')
   pl.plot(data[:,1], data[:,0])

I found one function, set_autoscalex_on(FALSE) but I'm not sure how to use it or whether it is what I want. Thanks

Honesta
  • 1,394
  • 1
  • 11
  • 20
  • That is what axes are for... Small values are close to the origin and large values are further off. Ask yourself what do you want to plot? Maybe you want to change x and y = f(x) ... – Jan Feb 08 '13 at 10:28
  • why do you want to do that? – Francesco Montesano Feb 08 '13 at 10:28
  • I have to plot some data for the last 30 days, and that corrupts the chart, I'll try to add the plot here so you can see – Honesta Feb 08 '13 at 10:36
  • Do you want two separate lines for `data[:,1]` and `data[:,0]` – Thorsten Kranz Feb 08 '13 at 10:38
  • No no, lines are fine, just the axis starts from 1 until 31, whereas I dont want to do that since I'm plotting everyday the last 30 days, that means that the chart should start from 30 days ago until now. So, kinda like this: it should start with January 6, 7, 8, .... 31, 0, 1,2,3,4,5,6,7,8 (that is today) – Honesta Feb 08 '13 at 10:40
  • you should update (edit) your question if you are talking about datetimes (to make it clear to the readers) – bmu Feb 08 '13 at 11:25
  • 1
    `set_autoscalex_on(FALSE)` set the authomatic adjustment the axis limits – Francesco Montesano Feb 08 '13 at 13:09

2 Answers2

5

You could provide a dummy x-range, and then override the xtick labels. I do agree with the comments above questioning wether its the best solution, but thats hard to judge without any context.

If you really want to, this might be an option:

fig, ax = plt.subplots(1,2, figsize=(10,4))

x = [2,4,3,6,1,7]
y = [1,2,3,4,5,6]

ax[0].plot(x, y)

ax[1].plot(np.arange(len(x)), y)
ax[1].set_xticklabels(x)

enter image description here

edit: If you work with dates, why not plot the real date on the axis (and perhaps format it by the day-of-month if you do want 29 30 1 2 etc on the axis?

Rutger Kassies
  • 61,630
  • 17
  • 112
  • 97
  • thanks, that proved helpful to me in a situation where I wanted to plot with a categorical x axis (the names of rooms), but I wanted to order it such that the y values (exhibits per room) would be increasing to make it easy on the eye. – Alex Dec 15 '13 at 01:01
  • 1
    For others reference, if you have a long list for the x axis and need to show all the elements, pls refer to http://stackoverflow.com/questions/26131822/how-to-display-all-label-values-in-matlibplo – Kevin Sep 30 '14 at 23:54
  • In my case the code in the answer didn't work, I was getting the wrong labels in the wrong place. But as suggested by @Kevin, the code in https://stackoverflow.com/questions/26131822/how-to-display-all-label-values-in-matplotlib worked perfectly. – Homero Esmeraldo Mar 31 '18 at 19:11
3

Maybe you want to set the xticks:

import pylab as pl
data = genfromtxt('myfile.dat')
pl.axis('auto')
xs = pl.arange(data.shape[0])
pl.plot(xs, data[:,0])
pl.xticks(xs, data[:,1])

Working sample:

Another option would be to work with datetimes. If you work with dates, you can use those as input to the plot command.

Working sample:

import random
import pylab as plt
import datetime
from matplotlib.dates import  DateFormatter, DayLocator

fig, ax = plt.subplots(2,1, figsize=(6,8))

# Sample 1: use xticks
days = [29,30,31,1,2,3,4,5]
values = [random.random() for x in days]

xs = range(len(days))

plt.axes(ax[0])
plt.plot(xs, values)
plt.xticks(xs, days)

# Sample 2: Work with dates
date_strings = ["2013-01-30",
                "2013-01-31",
                "2013-02-01",
                "2013-02-02",
                "2013-02-03"]

dates = [datetime.datetime.strptime(x, "%Y-%m-%d") for x in date_strings]
values = [random.random() for x in dates]

plt.axes(ax[1])
plt.plot(dates,values)
ax[1].xaxis.set_major_formatter(DateFormatter("%b %d"))
ax[1].xaxis.set_major_locator(DayLocator())
plt.show()

Sample

Thorsten Kranz
  • 12,492
  • 2
  • 39
  • 56
  • I'll try this code and get back to you. (though I don't understand it) thanks – Honesta Feb 08 '13 at 10:52
  • I'll be glad to help you understand it. – Thorsten Kranz Feb 08 '13 at 11:01
  • Wow thanks a lot. It worked. though now I got a little issue with the X axis, it got too much crowded :). As you can see http://imgur.com/AD1vbbO If you have any advice on what to search ,say, name of the function, that would be appreciated a lot. – Honesta Feb 08 '13 at 11:05
  • or to increase the size of the chart so it can fit all date, anyone knows? – Honesta Feb 08 '13 at 11:06
  • 1
    nevermind I just found this way fig = pl.gcf() fig.set_size_inches(18.5,10.5) fig.savefig('test2png.png',dpi=100) – Honesta Feb 08 '13 at 11:15
  • You can control the number of ticks on an axis by specifying an TickLocator. In my code, I use an `DayLocator`. Look at [this page](http://matplotlib.org/examples/pylab_examples/finance_demo.html), there are other sample for locators related to dates. – Thorsten Kranz Feb 08 '13 at 11:55