You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用matplotlib.pyplot设置Y轴刻度间隔?PowerBI Python绘图问题

解决PowerBI中Python柱状图Y轴刻度间隔设置问题

要让Y轴刻度按10的间隔显示,你需要手动指定刻度位置或使用刻度定位器,修改后的代码如下:

import matplotlib.pyplot as plt
import numpy as np

ax=plt.gca()

dataset.plot(kind='bar',x='Day',y=['Feed_%Diff_From_Stand','Feed_Req_to_Standard'], color=['red','green'],ax=ax)

plt.ylim(bottom= -100, top= 100)
# 方法1:直接生成指定刻度
plt.yticks(np.arange(-100, 101, 10))
# 或者方法2:使用刻度定位器(无需numpy)
# from matplotlib.ticker import MultipleLocator
# ax.yaxis.set_major_locator(MultipleLocator(10))

plt.xlabel('Days')

plt.show()

说明:

  • 方法1通过np.arange(-100, 101, 10)生成从-100到100、步长为10的刻度数组,直接传给plt.yticks()强制显示这些刻度。
  • 方法2导入MultipleLocator并设置Y轴主刻度间隔为10,这种方式更灵活,轴范围变化时能自动适配,且无需依赖numpy。

若在PowerBI中运行方法1,需确保已安装numpy库;方法2则仅依赖matplotlib,适配性更强。

内容的提问来源于stack exchange,提问作者Imtisal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 01:40:34