更改x轴刻度

1 投票
3 回答
1498 浏览
提问于 2025-05-01 18:23

我想画一个图,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

只需用 (x, y) 来绘制图形,x 轴就是实际的数值,并且这些数值是均匀间隔的,如果你是这个意思的话?

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

那这样做怎么样呢?(这是保罗·伊万诺夫的例子)

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

在这里输入图片描述

撰写回答