pepe1
pepe1

Reputation: 235

How to have spacing between x axis ticks be consistent when a line graph doesn't start at the first x axis tick

Originally I had a single graph on one plot:

x1 = [0,1,2,3,10,11]
y1 = [1,2,3,4,5,6]
ax.plot(x1,y1)

But this caused the spacing between x axis label to be inconsistent as shown here:

enter image description here

To fix that instead I did

ax.plot(y1)
ax.set_xticklabels(x1)

Which then changed the graph to this (which is what I wanted):

enter image description here

But now let's say I have another line graph which is y2 = [2,3,4,5,6,7] and I want to add this graph to the same plot BUT I want to offset it such that it starts at x = 2 for example.

When I say ax.plot(y2) it will start at the default first tick (x=1) and if I say ax.plot(x2,y2) the 2nd line graph will have the same issue that Graph1 had.

Basically I want the end result to look like two parallel lines but one of them starts at x = 2. Any suggestions would be much appreciated, thanks!

Upvotes: 1

Views: 471

Answers (2)

Rantanplan
Rantanplan

Reputation: 11

I would suggest that you plot y2 over a subset of x1:

ax.plot(x1[2:5],y2)

of cause y2 would have to have the same length as the subset of x1, which is not the case at the moment. Alternatively you could make a list of x-values for each set of y-values.

Upvotes: 0

DavidG
DavidG

Reputation: 25371

If, as you say in the question you actually want to plot y2, starting at x=2, then the two lines would overlap.

fig,ax = plt.subplots()

ticks = [0,1,2,3,10,11]

y1 = [1,2,3,4,5,6]
y2 = [2,3,4,5,6,7]

x1 = np.arange(1,len(y1)+1,1)
x2 = np.arange(2,len(y2)+2,1)

ax.plot(x1,y1)
ax.plot(x2,y2)

ax.set_xticklabels(ticks)

plt.show()

Produces:

enter image description here

You say in the end that you would like two parallel lines, but with one starting at x = 2. If so, then you can do the same as above, but use y1 instead:

ax.plot(x2,y1)

which gives:

enter image description here

Upvotes: 1

Related Questions