带有对数轴的 matplotlib 图形,但没有科学/指数符号 [英] matplotlib figure with logarithmic axis but ticks without scientific/exponential notation

查看:31
本文介绍了带有对数轴的 matplotlib 图形,但没有科学/指数符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个图形,其中 x 轴应以对数间隔排列,但我想手动设置刻度标签,并且我希望刻度标签以普通的%.2f"表示法出现,而不是指数表示法.以下解决方案基于

解决方案

ScalarFormatter 的使用肯定是可能的.然后,您需要确保未显示此问题中所示的次要刻度标签:

I am making a figure where the x-axis should be logarithmically spaced, but I want to manually set the tick labels, and I want the tick labels to appear in ordinary '%.2f' notation, not exponential notation. The following solution based on Matplotlib - logarithmic scale, but require non-logarithmic labels suggests to use ScalarFormatter, but does not work with matplotlib 2.0:

x = np.logspace(2, 3, 100)
y = x

fig, ax = plt.subplots(1, 1)
xscale = ax.set_xscale('log')
ax.set_xticks((100, 200, 300, 500))
xlim = ax.set_xlim(100, 1000)

from matplotlib.ticker import ScalarFormatter
ax.get_xaxis().set_major_formatter(ScalarFormatter())

__=ax.plot(x, y)

解决方案

The use of a ScalarFormatter is sure possible. You would then need to make sure that no minor ticklabels are shown as seen in this question: Matplotlib: setting x-limits also forces tick labels?

In your case the code would then look like:

import matplotlib.pyplot as plt
import numpy as np

x = np.logspace(2, 3, 100)
y = x

fig, ax = plt.subplots(1, 1)
xscale = ax.set_xscale('log')
ax.set_xticks((100, 200, 300, 500))
xlim = ax.set_xlim(100, 1000)

import matplotlib.ticker

ax.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())
ax.get_xaxis().set_minor_formatter(matplotlib.ticker.NullFormatter())

__=ax.plot(x, y)

plt.show()

这篇关于带有对数轴的 matplotlib 图形,但没有科学/指数符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆