图表:X轴刻度过多

12

第一个加载的图表在X轴上有太多的刻度(参见image01)。

enter image description here

如果我在X轴上使用缩放操作,那么该图表现在就可以正常加载了。

enter image description here

您能否给我一些建议,因为Plot构造参数似乎很好。

date_range = (735599.0, 735745.0)
x = (735610.5, 735647.0, 735647.5, 735648.5, 735669.0, 735699.0, 735701.5, 735702.5, 735709.5, 735725.5, 735728.5, 735735.5, 735736.0)
y = (227891.25361545716, 205090.4880046467, 208352.59317388065, 175462.99296699322, 98209.836461969651, 275063.37219361769, 219456.93600708069, 230731.12613806152, 209043.19805037521, 218297.51486296533, 208036.88967207001, 206311.71988471842, 216036.56824433553)
y0 = 218206.79192
x_after = (735610.5, 735647.0, 735647.5, 735701.5, 735702.5, 735709.5, 735725.5, 735728.5, 735735.5, 735736.0)
y_after = (227891.25361545716, 205090.4880046467, 208352.59317388065, 219456.93600708069, 230731.12613806152, 209043.19805037521, 218297.51486296533, 208036.88967207001, 206311.71988471842, 216036.56824433553)
linex = -39.1175584541
liney = 28993493.5251

ax.plot_date(x, numpy.array(y) / y0, color='r', xdate=True, marker='x')
ax.plot_date(x_after, numpy.array(y_after) / y0, color='r', xdate=True)
ax.set_xlim(date_range)
steps = list(ax.get_xlim())
steps.append(steps[-1] + 2)
steps = [steps[0] - 2] + steps
ax.plot(steps, numpy.array([linex * a + liney for a in steps]) / y0, color='b')

感谢你的帮助。 曼努埃尔


我非常确定你想要的是 http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.xticks。你可以在 matplotlib 图库中找到用法示例。 - wwii
当我观察get_xticks()时,一开始一切都很好,但在调用set_xlim(date_range)之后,我的X轴上有太多的刻度。 - Manuel Campomanes
2个回答

5

如果您有太多的xtick标签,以至于它们在绘图中全部混杂在一起,您可以使用pyplot.xticks将它们减少。参数是标签应用的点、标签本身和一个可选的旋转角度。

import numpy as np
import matplotlib.pyplot as plt
y = np.arange(10000)
ticks = y - 5000
plt.plot(y)
k = 1000
ys = y[::k]
ys = np.append(ys, y[-1])
labels = ticks[::k]
labels = np.append(labels, ticks[-1])
plt.xticks(ys,labels, rotation='vertical')
plt.show()
plt.close()

enter image description here


请使用X轴日期完成此操作。 - john stamos
@johnstamos - 不确定你的评论意味着什么 - 概念是相同的,无论是日期还是数字,您都可以选择数据子集作为刻度位置/标签。 - wwii
@johnstamos- 这个问答已经很老了,有许多方法可以解决这个问题。例如Figure.autofmt_xdate - 看起来是一个简洁的处理方式。也许需要一个新的答案,但是有很多SO Q&A处理这个问题。 - wwii

0

我不确定我完全理解你想做什么,但是旋转你的xticklabels是否足够?

# Add this code at the end of your script
# It will rotate the labels contained in your date range
plt.xticks(rotation=70)

如果我测试你的代码,我有7个标签,但旋转参数被更改为0(水平)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接