gpt4 book ai didi

python-3.x - 如何在离线绘图中绘制垂直线?

转载 作者:行者123 更新时间:2023-12-04 22:28:09 24 4
gpt4 key购买 nike

如何使用 python 在离线绘图中绘制一条垂直线?我想在 x=20、x=40 和 x=60 处添加线,所有这些都在同一个图中。

def graph_contracts(self):
trace1 = go.Scatter(
x=np.array(range(len(all_prices))),
y=np.array(all_prices), mode='markers', marker=dict(size=10, color='rgba(152, 0, 0, .8)'))
data = [trace1]
layout = go.Layout(title='Market Contracts by Period',
xaxis=dict(title='Contract #',
titlefont=dict(family='Courier New, monospace', size=18, color='#7f7f7f')),
yaxis=dict(title='Prices ($)',
titlefont=dict(family='Courier New, monospace', size=18, color='#7f7f7f')))
fig = go.Figure(data=data, layout=layout)
py.offline.plot(fig)

Generated graph

最佳答案

您可以添加 line s 通过 shape layout ,例如

import plotly
plotly.offline.init_notebook_mode()
import random

x=[i for i in range(100)]
trace = plotly.graph_objs.Scatter(x=x,
y=[random.random() for _ in x],
mode='markers')
shapes = list()
for i in (20, 40, 60):
shapes.append({'type': 'line',
'xref': 'x',
'yref': 'y',
'x0': i,
'y0': 0,
'x1': i,
'y1': 1})

layout = plotly.graph_objs.Layout(shapes=shapes)
fig = plotly.graph_objs.Figure(data=[trace],
layout=layout)
plotly.offline.plot(fig)

会给你

enter image description here

关于python-3.x - 如何在离线绘图中绘制垂直线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46839585/

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