更改x轴的刻度

时间:2014-11-05 09:11:49

标签: python matplotlib

我想绘制一个x轴值= [151383,151433,175367,178368,183937]的图表 相应的y轴值= [98,96,95,100,90]

X轴值不是固定间隔。但我希望x轴应该是间隔的规则。 如果我只是写

matplotlib.pyplot(y) 

那么间隔是规则的,x轴是[1,2,3,4,5] 如何将其更改为实际的x轴值?

3 个答案:

答案 0 :(得分:1)

我想这就是你要找的东西:

>>> 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

答案 1 :(得分:0)

只是用(x,y)绘制,x轴是有规律间隔的实际值,如果那是你要问的那个?

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

答案 2 :(得分:0)

做这样的事情怎么样? (这是Paul Ivanov的例子)

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()