我尝试在matplotlib中绘制一些数据,以显示实验的结果,如下所示:
xvalues = [2, 4, 8, 16, 32, 64, 128, 256]
yvalues = [400139397.517, 339303459.4277, 296846508.2103, 271801897.1163,
295153640.7553, 323820220.6226, 372099806.9102, 466940449.0719]我希望在对数刻度上绘制它,以使其更容易可视化,因此我编写了以下代码:
import matplotlib.pyplot as plt
def plot_energy(xvalues, yvalues):
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(xvalues, yvalues)
ax.plot(xvalues, yvalues)
ax.set_xscale('log')
ax.set_xticklabels(xvalues)
ax.set_xlabel('RUU size')
ax.set_title("Energy consumption")
ax.set_ylabel('Energy per instruction (nJ)')
plt.show()但是,正如您所看到的,我的xlabels并没有像我希望的那样出现,如下所示

如果我删除行ax.set_xticklabels(xvalues),那么我会得到以下结果,这也不是我想要的结果:

如果能帮助我在x轴上绘制正确的值,我将非常感激!
提前谢谢。
发布于 2013-02-01 19:24:05
您仅更改记号的标签,而不更改记号位置。如果您使用:
ax.set_xticks(xvalues)看起来是这样的:

大多数情况下,如果你想要一些完全不同的东西,比如类别标签,你只需要设置(覆盖)标签。如果你想坚持使用轴上的实际单位,最好使用(如果需要)自定义格式化程序设置刻度位置。
发布于 2013-02-01 19:29:00
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
def plot_energy(xvalues, yvalues):
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(xvalues, yvalues)
ax.semilogx(xvalues, yvalues, basex = 2)
ax.xaxis.set_major_formatter(ticker.ScalarFormatter())
ax.set_xlabel('RUU size')
ax.set_title("Energy consumption")
ax.set_ylabel('Energy per instruction (nJ)')
plt.show()
xvalues = [2, 4, 8, 16, 32, 64, 128, 256]
yvalues = [400139397.517, 339303459.4277, 296846508.2103, 271801897.1163,
295153640.7553, 323820220.6226, 372099806.9102, 466940449.0719]
plot_energy(xvalues, yvalues)收益率

https://stackoverflow.com/questions/14645134
复制相似问题