gpt4 book ai didi

python - 如何在 ScalarFormatter 之后更改双对数图的 xticks 和 yticks?

转载 作者:行者123 更新时间:2023-12-01 01:18:39 26 4
gpt4 key购买 nike

我有这个代码:

 # Modules I import 

import matplotlib
if os.environ.get('DISPLAY','') == '':
print('no display found. Using non-interactive Agg backend')
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter
from pylab import *

# My Variables to plot

ideal_walltime_list = [135.82, 67.91, 33.955, 16.9775, 8.48875]
cores_plotting = [16, 32, 64, 128, 256]
time_plotting = [135.82, 78.69, 50.62, 46.666, 42.473]

# My plotting part
plt.figure()
plt.scatter(cores_plotting, time_plotting, c='r', label='System wall time')
plt.plot(cores_plotting, ideal_walltime_list, c='k', label='Ideal Wall time')

plt.title('109bEdec_test')
plt.ylabel('Wall time (s)')

plt.xlabel('Cores')
plt.legend(loc='upper right')
plt.yscale('log')
plt.xscale('log')

ax = gca().xaxis
ax.set_major_formatter(ScalarFormatter())
ax.set_minor_formatter(ScalarFormatter())

ay = gca().yaxis
ay.set_major_formatter(ScalarFormatter())
ay.set_minor_formatter(ScalarFormatter())
plt.savefig('109bEdec_test' + '.png',dpi=1800)


plt.show()

当我运行此代码时,我的绘图如下所示:

enter image description here

但是,我需要 x 轴和 y 轴来显示与 cores_plotting 变量相对应的刻度,而不是所有格式错误的数字。我尝试过使用:

plt.xticks(cores_plotting)
plt.yticks(cores_plotting)

但没有成功。

我也尝试过:

plt.xticks(cores_plotting, ('16', '32', '64', '128', '256'))
plt.yticks(cores_plotting, ['16', '32', '64', '128', '256'])

但也没有成功。现在我只需要将 cores_plotting 项目作为我的 X 和 Y 刻度即可。

我的python版本是3.6.5,我的Matplotlib版本是3.0.2。

谢谢!

最佳答案

您可以首先放置将充当主要刻度的自定义刻度,然后隐藏次要刻度。您需要创建一个轴句柄 ax 来访问小刻度。请检查您用于 y 刻度标签的字符串。

fig, ax = plt.subplots()
plt.scatter(cores_plotting, time_plotting, c='r', label='System wall time')
plt.plot(cores_plotting, ideal_walltime_list, c='k', label='Ideal Wall time')

# Rest of your code here

ax.set_yticks(cores_plotting)
ax.set_yticklabels(['16', '32', '64', '128', '256'])

ax.set_xticks(cores_plotting)
ax.set_xticklabels(['16', '32', '64', '128', '256'])

for xticks in ax.xaxis.get_minor_ticks():
xticks.label1.set_visible(False)
xticks.set_visible(False)

for yticks in ax.yaxis.get_minor_ticks():
yticks.label1.set_visible(False)
yticks.set_visible(False)

Matplotlib版本问题

似乎以下命令在 matplotlib 3+ 版本中不起作用并抛出

TypeError: 'list' object is not callable` error.

在这种情况下,请使用上述方法分配刻度和刻度标签。

plt.xticks(cores_plotting, ['16', '32', '64', '128', '256']);
plt.yticks(cores_plotting, ['16', '32', '64', '128', '256'])

enter image description here

关于python - 如何在 ScalarFormatter 之后更改双对数图的 xticks 和 yticks?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54066971/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com