【问题标题】:Create a plotly line graph where the line is colored by a category?创建一个曲线图,其中线条按类别着色?
【发布时间】:2018-11-20 01:47:35
【问题描述】:

我需要创建一个由分类数据列着色的简单曲线图。数据是需要按类别着色的时间序列数据。有谁知道如何使用 python plotly api 在简单的折线图或时间序列图中按类别设置颜色类别?

x_axes - 时间数据 y_axes - 从 0' 到 5000' 的深度数据 类别 - on_bottom、off_bottom、钻孔等。

输出示例如下图所示,按上面列出的类别列着色?

Plotly Python - Time Series Graph Example

【问题讨论】:

    标签: python-3.x plotly plotly-dash


    【解决方案1】:

    您需要对数据进行分组并在图表中以不同的轨迹显示它们。您可以使用DataFrame Subsetting 来执行此操作。做子集的主线就是这样。

    df[df['direction'] == 'Increasing']['AAPL.Open']
    

    df[df['direction'] == 'Increasing'] 部分发生的情况是,我们检查数据帧的direction 列是否等于Increasing 值/类别,如果为真,则对数据帧进行子集化,以便仅存在这些值,然后我们可以通过使用['AAPL.Open']部分选择列来选择要绘制的特定列

    请参考以下示例,如果您的问题得到解决,请告诉我!

    代码:

    import plotly.offline as py
    import plotly.graph_objs as go
    from plotly.offline import init_notebook_mode, iplot, plot
    from plotly import tools
    import pandas as pd
    import numpy as np
    init_notebook_mode(connected=True)
    
    df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv")
    
    opening_increasing = go.Scatter(
                    x=df.Date,
                    y=df[df['direction'] == 'Increasing']['AAPL.Open'],
                    name = "AAPL Opening Price - Increasing",
                    line = dict(color = '#17BECF'),
                    opacity = 0.8)
    
    opening_decreasing = go.Scatter(
                    x=df.Date,
                    y=df[df['direction'] == 'Decreasing']['AAPL.Open'],
                    name = "AAPL Opening Price - Decreasing",
                    line = dict(color = '#7F7F7F'),
                    opacity = 0.8)
    
    data = [opening_increasing, opening_decreasing]
    
    layout = dict(
        title = "Apple Opening Price by Increasing/Decreasing Categories of Direction"
    )
    
    fig = dict(data=data, layout=layout)
    py.iplot(fig, filename = "Manually Set Range")
    

    输出:

    【讨论】:

      猜你喜欢
      • 2018-12-02
      • 2017-09-27
      • 2021-12-28
      • 2014-07-02
      • 1970-01-01
      • 2013-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多