我正在使用以下代码在Python中创建一个简单的可绘制折线图。我有两个变量(在代码底部):
ctime
amount
ctime仅使用每个元素的当前时间;有十次 数量是包含在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 ] )
答案 0 :(得分:0)
因此,对于您的用例,您需要使用折线图下的属性marker.color
,该属性在官方文档中以
颜色(颜色)
设置标记颜色。它接受特定的颜色或映射到 相对于数组的最大值和最小值或相对值的色标 设置为cmin
和cmax
。
了解更多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')
输出: