以 5 分钟的间隔绘制条形图并添加一条线

Plotting bars with 5 min interval and adding a line

我正在尝试根据正面和负面情绪绘制收盘价。我能够将其绘制为下图;但是,条形图的颜色显示不正确。有什么想法可以改变它们吗?

from plotly.subplots import make_subplots
import plotly.graph_objects as go

fig2 = make_subplots(specs=[[{"secondary_y": True}]])
fig2.add_trace(go.Scatter(x=data.index,y=data['close'],name='Price'),secondary_y=False)
fig2.add_trace(go.Bar(x=data.index,y=data['pos'],name='Positive'),secondary_y=True)
fig2.add_trace(go.Bar(x=data.index,y=data['neg'],name='Negative'),secondary_y=True)

fig2.show()

  • 从你的代码中暗示了你的数据帧结构,并使用 plotly 财务样本数据集作为起点
  • 关于布局的两件事
    1. 使关闭跟踪前面的主跟踪
    2. 审查 bargroup 参数并将 bargap 减少到零
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import pandas as pd

df = pd.read_csv(
    "https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv"
)

# make plotly dataset compatible with OP implied structure
data = df.set_index(pd.date_range("1-Jan-2022", freq="5Min", periods=len(df))).rename(
    columns={"AAPL.Close": "close", "dn": "neg", "up": "pos"}
)

fig2 = make_subplots(specs=[[{"secondary_y": True}]])
fig2.add_trace(
    go.Scatter(x=data.index, y=data["close"], name="Price"), secondary_y=False
)
fig2.add_trace(go.Bar(x=data.index, y=data["pos"], name="Positive"), secondary_y=True)
fig2.add_trace(go.Bar(x=data.index, y=data["neg"], name="Negative"), secondary_y=True)


# a few changes to make layout work better
#  1. put close at front
#  2. reduce "whitespace" in bars
fig2.update_layout(
    yaxis={"overlaying": "y2"}, yaxis2={"overlaying": None}, barmode="overlay", bargap=0
)