在线/面积图的左右两侧添加填充。

6

如何在折线/面积图的左右两侧添加填充?

这是我的图表样式:

enter image description here

这是应该呈现的样子(填充/边距部分):

enter image description here

2个回答

5

修改轴范围的最佳方法似乎是使用fig.update_xaxes()。而您如何进行修改将取决于您的数据大小和类型。以下是一个使用日期的示例,其中x轴范围在源数据的开头和结尾增加了一天:

图1:调整后的x轴

enter image description here

代码:

import pandas as pd
import plotly.graph_objects as go

# data
yVals = [3, 2, 3, 5, 2, 6]
days = len(yVals)
dates = pd.date_range('1/1/2020', periods=numdays)

# plotly figure
fig = go.Figure()
fig.add_trace(go.Scatter(x=dates,
                         y=yVals, 
                         fill='tozeroy')
             )

# adjustments
fig.update_xaxes(range=[dates[0]-1,dates[-1]+1])

fig.show()

图2: 未调整的x轴

enter image description here


谢谢。我正在使用https://plot.ly/javascript/,让我看看是否可以使用JS复制相同的内容。 - Silver Ringvee
@SilverRingvee 啊...我甚至没有注意到Python标签不存在。如果你仍然觉得我的建议有帮助,我会让它保留。如果不是,我也可以删除它。 - vestland
如何在不绘制“虚假”值的情况下处理Y轴? - mike01010

1
受 vestland 的 answer 启发,但使用 Javascript 实现。 Before

enter image description here

之后

enter image description here

代码

element = document.getElementById('traffic-overview');

data = [{
    x: ["2019-09-29", "2019-09-30", "2019-10-01", "2019-10-02", "2019-10-03", "2019-10-04", "2019-10-05", "2019-10-06", "2019-10-07", "2019-10-08", "2019-10-09", "2019-10-10", "2019-10-11", "2019-10-12", "2019-10-13", "2019-10-14", "2019-10-15", "2019-10-16", "2019-10-17", "2019-10-18", "2019-10-19", "2019-10-20", "2019-10-21", "2019-10-22", "2019-10-23", "2019-10-24", "2019-10-25", "2019-10-26", "2019-10-27", "2019-10-28"],
    y: [30, 66, 49, 154, 220, 224, 243, 164, 90, 71, 164, 199, 246, 251, 217, 85, 67, 156, 204, 218, 233, 204, 82, 59, 183, 205, 197, 219, 271, 116],
    fill: 'tozeroy',
    //fillcolor: '#9ed4fd',
    line: {
      color: '#008ffb'
    },
    //mode: 'lines',
    type: 'scatter'
}];

layout = {
    responsive: true,
    margin: { t: 10, b: 30, l: 30, r: 0 },
    xaxis: { range: ["2019-09-28", "2019-10-29"] } // <-- note this line!
};

Plotly.plot( element, data, layout );

{{链接1:JSFiddle}}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接