创建一个曲线图,其中线条按类别着色?

时间:2018-06-10 18:57:46

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

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

x_axes - 时间数据 y_axes - 深度数据从0'到5000' 类别 - on_bottom,off_bottom,钻井等

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

Plotly Python - Time Series Graph Example

1 个答案:

答案 0 :(得分:2)

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

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

df[df['direction'] == 'Increasing']部分中,我们会检查数据框的direction列是否等于Increasing值/类别,如果为true,则数据框是子集化的只有那些值存在,那么我们可以通过使用零件['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")

<强>输出:

enter image description here