简体   繁体   English

Matplotlib更改轴刻度

[英]Matplotlib changing axis ticks

I have a few lines of making a scatter plot out of a collection of points (x 0 ,y 0 ), ... (x n ,y n ). 我有几行根据点(x 0 ,y 0 ),...(x n ,y n )的集合制作散点图。

The truth is that I am only interested in the x scale as it is the scale on which the graph is power law behaved but after plotting I would like to replace the ticks on the x-axis by some other function of $x_i$ . 事实是,我只对x比例感兴趣,因为它是图形的幂律行为所依据的比例,但是在绘制之后,我想用$x_i$其他函数替换x轴上的刻度。

How could I do so? 我该怎么办?

You can tell matplotlib where to put the x-axis labels by 您可以告诉matplotlib将x轴标签放在哪里

ax.set_xticks(positions)

where positions is a list of tick positions in the scale which your data is using for x-positioning and ax is the axis to plot on ( ax=plt.gca() or ax=fig.gca() ). 其中positions是数据用于x定位的刻度刻度位置列表,而ax是绘制坐标轴( ax=plt.gca()ax=fig.gca() )。 You can then set the tick labels: 然后可以设置刻度标签:

ax.set_xticklabels(labels)

Example: you want to display x-ticks in log scale but still use a linear scale for the x-axis (not very useful, I know): 示例:您想以对数刻度显示x刻度,但仍对x轴使用线性刻度(我知道不是很有用):

positions = np.array([1, 10, 100, 1000])
ax.set_xticks(positions)
ax.set_xticklabels(np.log10(positions))

Note however, that this won't change the x-scale of your data! 但是请注意,这不会改变数据的x比例!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM