Alan.G
Alan.G

Reputation: 113

Change the ticks of x-axis

I want to plot a graph which has x-axis values = [151383,151433,175367,178368,183937] corresponding y-axis values = [98,96,95,100,90]

X-axis values are not at regular intervals. But I want x-axis should be at regular at interval. if i just write

matplotlib.pyplot(y) 

then the interval is regular and x-axis comes as [1,2,3,4,5] how can i change that to actual x-axis values?

Upvotes: 1

Views: 1523

Answers (3)

eivl
eivl

Reputation: 148

What about doing something like this? (This is Paul Ivanov's example)

import matplotlib.pylab as plt
import numpy as np

# If you're not familiar with np.r_, don't worry too much about this. It's just 
# a series with points from 0 to 1 spaced at 0.1, and 9 to 10 with the same spacing.
x = np.r_[0:1:0.1, 9:10:0.1]
y = np.sin(x)

fig,(ax,ax2) = plt.subplots(1, 2, sharey=True)

# plot the same data on both axes
ax.plot(x, y, 'bo')
ax2.plot(x, y, 'bo')

# zoom-in / limit the view to different portions of the data
ax.set_xlim(0,1) # most of the data
ax2.set_xlim(9,10) # outliers only

# hide the spines between ax and ax2
ax.spines['right'].set_visible(False)
ax2.spines['left'].set_visible(False)
ax.yaxis.tick_left()
ax.tick_params(labeltop='off') # don't put tick labels at the top
ax2.yaxis.tick_right()

# Make the spacing between the two axes a bit smaller
plt.subplots_adjust(wspace=0.15)

plt.show()

Upvotes: 0

Irshad Bhat
Irshad Bhat

Reputation: 8709

I guess this is what you are looking for:

>>> from matplotlib import pyplot as plt
>>> xval=[151383,151433,175367,178368,183937]
>>> y=[98, 96, 95, 100, 90]
>>> x=range(len(xval))
>>> plt.xticks(x,xval)
>>> plt.plot(x,y)
>>> plt.show()

enter image description here

Upvotes: 1

Anzel
Anzel

Reputation: 20553

Just plot with (x, y) and x-axis is the actual values with regular intervals, if that's what you're asking?

matplotlib.pyplot.plot(x, y) # with the plot by the way

Upvotes: 0

Related Questions