简体   繁体   English

如何设置x和y轴的对数刻度?

[英]How to set the ticks of log scale for x&y axis?

I want to plot a log scale graph without scientific notation. 我想在没有科学记数法的情况下绘制对数比例图。

    import matplotlib as mpl
    import matplotlib.pyplot as plt

    plt.plot(np.arange(0,10,0.1))

    plt.xscale('log')
    plt.yscale('log')
    plt.xlim(0.1,100)
    plt.ylim(1,10)

    plt.gca().xaxis.set_major_formatter(mpl.ticker.ScalarFormatter())
    plt.gca().yaxis.set_major_formatter(mpl.ticker.ScalarFormatter())
    plt.show()

Question: 题:

  1. Y axis still shows the format of scientific notation. Y轴仍然显示科学记数法的格式。 How to change it? 怎么改呢?

  2. How to make specific ticks for y axis? 如何为y轴制作特定的刻度? I tried plt.yticks([1,10]) , but it doesn't work. 我试过plt.yticks([1,10]) ,但它不起作用。

  3. How to get rid of the decimal point of ticks for both x and y axis? 如何摆脱x轴和y轴的刻度小数点?

    在此输入图像描述

1. Get rid of Scientific notation. 1.摆脱科学记数法。

The ticks are major and minor ticks, hence you would need to set the minor formatter as well: 刻度是主要和次要刻度,因此您还需要设置次要格式化程序:

plt.gca().yaxis.set_major_formatter(mpl.ticker.ScalarFormatter())
plt.gca().yaxis.set_minor_formatter(mpl.ticker.ScalarFormatter())

2. Show ticks at specific custom locations 2.在特定的自定义位置显示刻度

Getting rid of the minor ticklabels allows yticks to work as expected. 摆脱次要的ticklabel允许yticks按预期工作。

plt.yticks([1,10])
plt.gca().yaxis.set_minor_formatter(mpl.ticker.NullFormatter())

3. Getting rid of the decimal points 3.摆脱小数点

I suppose it does not make sense to get rid of the decimal points for a label like 0.1 . 我认为删除像0.1这样的标签的小数点是没有意义的。 Hence one would probably choose a StrMethodFormatter with the g eneral purpose numeric format g . 因此,人们可能会选择一个StrMethodFormatterG ENERAL目的数字格式g

plt.gca().yaxis.set_major_formatter(mpl.ticker.StrMethodFormatter("{x:g}"))
  1. Use plt.ticklabel_format(style='plain') to get rid of the scientific notation. 使用plt.ticklabel_format(style='plain')来摆脱科学记数法。

  2. It looks like plt.yticks([1,10]) did its job. 看起来像plt.yticks([1,10])完成了它的工作。 yticks() only adds the specific numbers you provide, not a range. yticks()仅添加您提供的特定数字,而不是范围。 So the ticks that it added were at y=1 and y=10, which are exactly at the bottom and top edges of your graph. 因此它添加的刻度位于y = 1和y = 10,它们恰好位于图形的底部和顶部边缘。 If you want to have more ticks between those, you can try plt.yticks(np.arange(1,10, step=d)) where 'd' is the distance you want between each step. 如果你想在它们之间有更多的刻度,你可以尝试plt.yticks(np.arange(1,10, step=d))其中'd'是你想要在每一步之间的距离。

  3. Try plt.gca().xaxis.set_major_formatter(mpl.ticker.EngFormatter(places=0)) to get rid of the decimal points. 尝试plt.gca().xaxis.set_major_formatter(mpl.ticker.EngFormatter(places=0))去除小数点。

All this can be found in the matplotlib docs, though admittedly it takes some digging. 所有这些都可以在matplotlib文档中找到,但不可否认它需要一些挖掘。

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

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