如何设置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()
问题:
-
Y轴仍显示科学计数法的格式.如何更改?
Y axis still shows the format of scientific notation. How to change it?
如何对y轴进行特定刻度?我尝试了plt.yticks([1,10])
,但是没有用.
How to make specific ticks for y axis? I tried plt.yticks([1,10])
, but it doesn't work.
如何摆脱x和y轴刻度的小数点?
How to get rid of the decimal point of ticks for both x and y axis?
推荐答案
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.在特定的自定义位置显示刻度线
摆脱次要的滴答标签可以使yticks
正常工作.
Getting rid of the minor ticklabels allows yticks
to work as expected.
plt.yticks([1,10])
plt.gca().yaxis.set_minor_formatter(mpl.ticker.NullFormatter())
3.摆脱小数点
我想摆脱0.1
这样的标签的小数点是没有意义的.因此,人们可能会选择通用数字格式为 g 的StrMethodFormatter
.
I suppose it does not make sense to get rid of the decimal points for a label like 0.1
. Hence one would probably choose a StrMethodFormatter
with the general purpose numeric format g
.
plt.gca().yaxis.set_major_formatter(mpl.ticker.StrMethodFormatter("{x:g}"))
这篇关于如何设置x& y轴的对数刻度刻度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!