python - 基于变量值的自定义绘图标记

标签 python plotly

我正在使用以下代码在 Python 中创建一个简单的绘图折线图。我有两个变量(在代码底部):

ctime
amount

ctime 只使用数量中每个元素的当前时间;有10次
数量是包含 0-1000 的数量;这是十个金额

我想通过以下方式为我的绘图标记着色:

金额小于300;该值的特定标记将为绿色
金额在 300 到 400 之间;该值的特定标记将为黄色
金额大于400;该值的特定标记将为红色

有什么方法可以为此构建条件类型处理程序吗?
layout = Layout(
    title='Current Amount',
    titlefont=Font(
        family='"Open sans", verdana, arial, sans-serif',
        size=17,
        color='#444'
    ),
    font=Font(
        family='"Open sans", verdana, arial, sans-serif',
        size=12,
        color='#444'
    ),
    showlegend=True,
    autosize=True,
    width=803,
    height=566,
    xaxis=XAxis(
        title='Time',
        titlefont=Font(
            family='"Open sans", verdana, arial, sans-serif',
            size=14,
            color='#444'
        ),
        range=[1418632334984.89, 1418632334986.89],
        domain=[0, 1],
        type='date',
        rangemode='normal',
        autorange=True,
        showgrid=False,
        zeroline=False,
        showline=True,
        autotick=True,
        nticks=0,
        ticks='inside',
        showticklabels=True,
        tick0=0,
        dtick=1,
        ticklen=5,
        tickwidth=1,
        tickcolor='#444',
        tickangle='auto',
        tickfont=Font(
            family='"Open sans", verdana, arial, sans-serif',
            size=12,
            color='#444'
        ),
        mirror='allticks',
        linecolor='rgb(34,34,34)',
        linewidth=1,
        anchor='y',
        side='bottom'
    ),
    yaxis=YAxis(
        title='GHI (W/m2)',
        titlefont=Font(
            family='"Open sans", verdana, arial, sans-serif',
            size=14,
            color='#444'
        ),
        range=[-5.968375815056313, 57.068375815056314],
        domain=[0, 1],
        type='linear',
        rangemode='normal',
        autorange=True,
        showgrid=False,
        zeroline=False,
        showline=True,
        autotick=True,
        nticks=0,
        ticks='inside',
        showticklabels=True,
        tick0=0,
        dtick=1,
        ticklen=5,
        tickwidth=1,
        tickcolor='#444',
        tickangle='auto',
        tickfont=Font(
            family='"Open sans", verdana, arial, sans-serif',
            size=12,
            color='#444'
        ),
        exponentformat='B',
        showexponent='all',
        mirror='allticks',
        linecolor='rgb(34,34,34)',
        linewidth=1,
        anchor='x',
        side='left'
    ),
    legend=Legend(
        x=1,
        y=1.02,
        traceorder='normal',
        font=Font(
            family='"Open sans", verdana, arial, sans-serif',
            size=12,
            color='#444'
        ),
        bgcolor='rgba(255, 255, 255, 0.5)',
        bordercolor='#444',
        borderwidth=0,
        xanchor='left',
        yanchor='auto'
    )
)


new_data = Scatter(x=ctime, y=amount)
data = Data( [ new_data ] )

最佳答案

因此,对于您的用例,您需要使用属性 marker.color在折线图下,这是在官方文档中给出的。

color (color)
Sets the marker color. It accepts either a specific color or an array of numbers that are mapped to the colorscale relative to the max and min values of the array or relative to cmin and cmax if set.



阅读更多 here

下面是一个简单的工作示例,演示您的用例,请将其应用于您的解决方案,如果您的问题得到解决,请告诉我。
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs,init_notebook_mode,plot,iplot
init_notebook_mode(connected=True)

x = [1,2,3,4,5,6,7,8,9]
y = [100,200,300,400,500,600,700,800,900]

# function below sets the color based on amount
def SetColor(x):
    if(x < 300):
        return "green"
    elif(x >= 300 | x <= 400):
        return "yellow"
    elif(x > 400):
        return "red"

# Create a trace
trace = go.Scatter(
    x = x,
    y = y,
    marker = dict(color=list(map(SetColor, y)))
)

iplot([trace], filename='basic-line')

输出:

enter image description here

关于python - 基于变量值的自定义绘图标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51181729/

相关文章:

Python Plotly 将轴数格式化为 %

python - Plotly Python 中的刻度格式

python - 使用layout.updatemenus[] Dropdown动态更改X轴和Y轴标题

javascript - Plotly:基于 GeoJSON 文件创建 map

python - Pandas:将多个列名作为参数传递给具有 apply 的函数

python - 将 ConfigParser.items ('' ) 转换为字典

python - 在Python中循环二维数组的最有效方法是什么

python - 如果前两个字母在 python 中重复,则替换

python - 当uwsgi重新启动时,如何及时清理我的旧Python Worker中的资源而不延迟重新启动?

r - 如何在稀疏点之间插入数据以在 R 和plotly 中绘制等高线图