简体   繁体   English

如何使用 Pandas 在离线图中更改散点图样式

[英]How to change scatter plot style in offline plots with Pandas plotly

I'm plotting scatter plots from a Pandas dataframe using plotly and embedding them into html.我正在使用 plotly 从 Pandas 数据框中绘制散点图并将它们嵌入到 html 中。 I have figured out how to define the data, the layout and generate the code needed to embed but am struggling to find a way to change the style of plot.我已经弄清楚如何定义数据、布局并生成嵌入所需的代码,但我正在努力寻找改变绘图风格的方法。

Specifically, I'd like to:具体来说,我想:

  • Change the line style (eg from solid to dashed or dotted ... I figured out how to change from line to marker)更改线条样式(例如,从实线变为虚线或点状...我想出了如何从线条更改为标记)
  • Change the marker style and color更改标记样式和颜色
  • Specify the color of each line or marker series指定每条线或标记系列的颜色

Below is a snippet from my code showing the plotting section.下面是我的代码片段,显示了绘图部分。 This code works fine in my script, I just can't figure out how to modify the appearance.这段代码在我的脚本中运行良好,我只是不知道如何修改外观。 Any help would be great!任何帮助都会很棒!

Thanks :)谢谢 :)

layout = go.Layout(
    title="This is the title",
    xaxis=dict(
        title="x-axis label",
        autorange=True,
        showgrid=True,
        zeroline=False,
        showline=False,
        ticks='',
        showticklabels=True,
    ),
    yaxis=dict(
        title="y-axis label",
        autorange=True,
        showgrid=True,
        zeroline=False,
        showline=False,
        ticks='',
        showticklabels=True
    ),
    width=800,height=550
)

data=[
    go.Scatter(
        x=df["Timestamp"],
        y=df["Conditions1"],
        name="Trace 1",
        mode="markers",
        ),
    go.Scatter(
        x=df["Time"],
        y=df["Conditions2"],
        name="Trace 2",
        mode="markers"
        )
    ]

fig1 = go.Figure(data=data, layout=layout)

plot1 = plotly.offline.plot(fig1,
                            config={"displaylogo": False}, 
                            show_link=False, 
                            include_plotlyjs=True,
                            output_type='div')

If you want your plot to have dots and lines you should set mode="markers+lines" , either way inside the scatter you can modify the marker and line objects:如果您希望您的绘图具有点和线,您应该设置mode="markers+lines" ,无论哪种方式在散点内,您都可以修改markerline对象:

go.Scatter(
    x=df["Time"],
    y=df["Conditions2"],
    name="Trace 2",
    mode="markers+lines",
    marker=dict(
        color="red", # or "rgb(255,0,0)" or "#ff0000" or even another pandas column
        size=7,
        symbol="cross",
        line=dict(
            # you can add here the properties for the outline of the markers
            color="green",
            width=1,
        )
    )
    line=dict(
        shape="linear", # or "spline" for instance, for a curvy line
        dash="dash", # or "dot", "dashdot", etc.
        color="blue",
        width=3,
    )
)

you can see all the available options in the markers and lines reference.您可以在标记线条参考中看到所有可用选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM