4

I need to plot a sequence of y values against a sequence of x values. The x values varies in a very large range. pyplot seems using a linear x-axis. So the following code gives me a bad figure.

def bad_plot():
    x=[1,2,10,100,1000]
    y=[5,10,6,7,9]
    plt.plot(x,y,'rs')
    plt.show()                

enter image description here

The first three points (1,5), (2,10), (10,6) are so close. I want the x-axis only have 5 ticks [1,2,10,100,100] and they are uniformly scattered in the x-axis. How can I achieve that? Thanks a lot.

1
  • Are you looking for log-scaling? Commented Jan 22, 2015 at 5:37

1 Answer 1

5

For the record, I think this is a bad design choice for a graphic. Having non-regular (be them linear or log) ticks at regular intervals is confusing to the reader.

Okay what you simply have to do is plot against 0, 1, 2, 3, ... but then set the ticks to be your values of x at the same position using plt.xticks()

import numpy as np
import matplotlib.pyplot as plt

x=[1,2,10,100,1000]
y=[5,10,6,7,9]

N = len(x)
x2 = np.arange(N)

plt.plot(x2, y)

plt.xticks(x2, x)

plt.show()

Example

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. I agree your comment about confusion. In my use-case for now, the X-axis are just vary too much. For example, from [0,5,10,15,....100, 200, 300, 500, 1000,1500,...10000,20000,30000]. I tried to split the x-axis and plot into several figures. But then was asked to put in one figure.
@Peng yeah that's fair enough, I'd suggest that if you have some form of caption with the figure that you draw attention to it though maybe?
I would make that caveat much much larger and put it at the top. @PengZhang from your description I would go with either log-scale or a broken axis. What you are asking for is very mis-leading.
@tcaswell Thanks. I will make the description less misleading. I am quite new to plot figures. Maybe I don't even use the write terminology in desription. Could you show me how to use broken axis? I used to split it into several figures, each figure with reasonable x range.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.