python - PyLab : Plotting axes to log scale, 但在轴上标记特定点

标签 python graph matplotlib

基本上,我正在进行可扩展性分析,所以我使用的是 2、4、8、16、32 等数字,而图表看起来合理的唯一方式是使用对数刻度。

但不是通常的 10^1、10^2 等标记,我想在轴上指示这些数据点 (2,4,8...)

有什么想法吗?

最佳答案

有不止一种方法可以做到这一点,具体取决于您想要的灵 active /花哨程度。

最简单的方法就是做这样的事情:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

x = np.exp2(np.arange(10))

plt.semilogy(x)
plt.yticks(x, x)

# Turn y-axis minor ticks off 
plt.gca().yaxis.set_minor_locator(mpl.ticker.NullLocator())

plt.show()

enter image description here

如果你想以更灵活的方式来做,那么也许你可以使用这样的东西:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

x = np.exp2(np.arange(10))

fig = plt.figure()
ax = fig.add_subplot(111) 
ax.semilogy(x)
ax.yaxis.get_major_locator().base(2)
ax.yaxis.get_minor_locator().base(2)

# This will place 1 minor tick halfway (in linear space) between major ticks
# (in general, use np.linspace(1, 2.0001, numticks-2))
ax.yaxis.get_minor_locator().subs([1.5])

ax.yaxis.get_major_formatter().base(2)

plt.show()

enter image description here

或者像这样:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

x = np.exp2(np.arange(10))

fig = plt.figure()
ax = fig.add_subplot(111) 
ax.semilogy(x)
ax.yaxis.get_major_locator().base(2)
ax.yaxis.get_minor_locator().base(2)

ax.yaxis.get_minor_locator().subs([1.5])

# This is the only difference from the last snippet, uses "regular" numbers.
ax.yaxis.set_major_formatter(mpl.ticker.ScalarFormatter())

plt.show()

enter image description here

关于python - PyLab : Plotting axes to log scale, 但在轴上标记特定点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5605503/

相关文章:

python - 阻止 pygtk GUI 在长时间运行的过程中锁定

algorithm - 在给定顶点和一些限制的情况下查找图中的所有路径

python - 如何更改类中的变量并将其导入到另一个文件中?

python - 用 python 为 arm 交叉编译 gdb 失败

iphone - 使用 Core Graphics 绘制图形线

python - 使用 groupby 绘制选举捐赠信息的堆积条形图

python - 如何在seaborn中自定义热图的颜色条?

python - 调整条形图上的颜色编码,以便所有值在 matplotlib 中都正确编码

python - 如何使用具有逗号分隔符和空格的 pandas 解析 csv?

algorithm - 是否有一条从 a 城市到 b 城市的路线不超过 x 天?