Matplotlib 如何将 x 轴缩放 2 倍
Matplotlib how to scale the x axis by a factor of 2
我正在尝试在 matplotlib 中绘制一些东西,我希望 x 轴具有值。
0 2 4 8 16 32 64 256
plt.xticks 不起作用,因为它没有均匀地缩放 x 轴。它执行下面想象中的操作。
plt.xticks([0, 2, 4 ,8 ,16 ,32, 64, 128, 256])
有没有办法让想象看起来像下面这样的想象?
数字在底部均匀分布的地方是这样的吗?
如果您希望 matplotlib space 值均匀而不是根据数值,您需要将它们作为文本而不是数字。
以下代码在 x 以文本形式给出时给出直线,在 x 以数值形式给出时给出曲线。
import numpy as np
from matplotlib import pyplot as plt
# numerical values, spacing according to value, gives a curve
x = np.array([2,4,8,16,32,64,128,256])
# text values, spaced evenly, gives a straight line
x = np.array(['2','4','8','16','32','64','128','256'])
y = np.arange(1,9)
plt.title("Matplotlib demo")
plt.xlabel("x axis")
plt.ylabel("y axis")
plt.plot(x,y)
plt.show()
axis = fig.add_subplot()
ax.set_xscale("log")
ax.xaxis.set_ticklabels([0, 2, 4 ,8 ,16 ,32, 64, 128, 256])
ax.xaxis.set_ticks([0, 2, 4 ,8 ,16 ,32, 64, 128, 256])
ax.plot(x)
plt.show
我正在尝试在 matplotlib 中绘制一些东西,我希望 x 轴具有值。 0 2 4 8 16 32 64 256
plt.xticks 不起作用,因为它没有均匀地缩放 x 轴。它执行下面想象中的操作。
plt.xticks([0, 2, 4 ,8 ,16 ,32, 64, 128, 256])
有没有办法让想象看起来像下面这样的想象?
数字在底部均匀分布的地方是这样的吗?
如果您希望 matplotlib space 值均匀而不是根据数值,您需要将它们作为文本而不是数字。
以下代码在 x 以文本形式给出时给出直线,在 x 以数值形式给出时给出曲线。
import numpy as np
from matplotlib import pyplot as plt
# numerical values, spacing according to value, gives a curve
x = np.array([2,4,8,16,32,64,128,256])
# text values, spaced evenly, gives a straight line
x = np.array(['2','4','8','16','32','64','128','256'])
y = np.arange(1,9)
plt.title("Matplotlib demo")
plt.xlabel("x axis")
plt.ylabel("y axis")
plt.plot(x,y)
plt.show()
axis = fig.add_subplot()
ax.set_xscale("log")
ax.xaxis.set_ticklabels([0, 2, 4 ,8 ,16 ,32, 64, 128, 256])
ax.xaxis.set_ticks([0, 2, 4 ,8 ,16 ,32, 64, 128, 256])
ax.plot(x)
plt.show