python-3.x - 创建一个绘制线图,其中线按类别着色?

标签 python-3.x plotly plotly-dash

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

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

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

Plotly Python - Time Series Graph Example

最佳答案

您需要对数据进行分组并在图表中的不同轨迹中显示它们。您可以使用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

关于python-3.x - 创建一个绘制线图,其中线按类别着色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50787068/

相关文章:

javascript - 不需要的括号不断从我的函数返回 (JavaScript/html)

r - Plotly map 不在 R 中渲染

python - 使用 Dash,有没有办法在页面的两个部分中重复相同的 id 组件?

python - 阴谋冲刺 : How to integrate SHAP values

python - Plotly:如何为具有完全不同数据和布局的图形制作一个 plotly 下拉菜单?

python - urllib3.urlencode 字符串中的 googlescholar url

mysql - 使用 Flask 从 WTForms SelectField 检索键值

r - 包装长轴标签

python - 两个对象之间的循环引用是否需要使用weakref?

python - 如何使用 OpenCV 处理的图像将小图像粘贴到 python 中心的另一个大图像上?