python - Matplotlib set_xticklabels 未对齐

标签 python matplotlib

我有一个双 y 轴图,我无法让 xticks 标签工作。
这是我不修改 xticks 的时候: enter image description here 这是我做的时候: enter image description here

neurons = [4, 8, 16, 32, 64, 128]
fig, ax = plt.subplots(figsize=(13,7))
ax2 = ax.twinx()
# adding the plots
ax.grid(False)
ax2.grid(False)
ax.set_xticklabels(neurons)
ax.set_yticklabels(np.arange(250, 2500,250),fontsize=14)
ax2.set_yticklabels(np.arange(0, 120,20),fontsize=14)
ax.set_ylabel('Training time [s]', fontsize=18)
ax2.set_ylabel('Model avg accuracy [%]', fontsize=18)
ax.set_xlabel('Number of neurons', fontsize=18)
ax2.legend(fontsize=15)
plt.show()

最佳答案

您正在将新的 x 刻度标签设置到自动生成的 xtick 上。您可能希望将刻度位置设置为 neruons 的值:

ax.set_xticks(neurons)
ax.set_xticklabels(neurons)

它给出了类似的东西

enter image description here

关于python - Matplotlib set_xticklabels 未对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48705122/

相关文章:

python ctypes将指向结构的指针作为参数发送给 native 库

python - 为什么我会收到此错误?类型错误 : iter() returned non-iterator of type 'NoneType'

python numpy scipy griddata 是 nan 或所有相同的值

python - 导出为 PDF 时如何避免使用 matplotlib 调整图形大小?

python - 用于橡皮筋的鼠标左键和用于平移 PYQT 的中间键

python - 如何将选择显示名称传递给 Django REST 框架中的模型序列化?

python - 两个元组的最大值

python - 使用循环绘制多个图,然后组合这些图

python - 重叠艺术家的 Matplotlib 选择事件顺序

python - 使用 Matplotlib Python 创建箱线图