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

使用python-nvd3绘制lineChart时自定义X轴Y轴取值范围的方法

python-nvd3 lineChart自定义坐标轴范围方法

要自定义X轴和Y轴的取值范围,只需在初始化lineChart实例时,通过xAxis和yAxis参数指定forceX、forceY属性即可,属性值为[最小值, 最大值]格式的数组。

以下是对应需求的完整修改后代码:

from nvd3 import lineChart
chart = lineChart(
    width=600,
    # 自定义X轴范围为-1到13
    xAxis = {"forceX": [-1, 13]},
    # 自定义Y轴范围为0到12
    yAxis = {"forceY": [0, 12]}
)

xdata = range(13)
ydata = [1, 2, 3, 5, 8, 10, 11, 10, 8, 5, 4, 2, 1]

chart.add_serie(y=ydata, x=xdata)
chart.buildhtml()

with open('plot.html', 'w') as f:
    f.write(chart.htmlcontent)

修改后重新运行生成的plot.html,就会显示指定的坐标轴范围,不会再自动根据数据推导范围。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 13:54:03