我正在使用以下代码在 Python 中创建一个简单的绘图折线图。我有两个变量(在代码底部):
ctime
amount
Run Code Online (Sandbox Code Playgroud)
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 ] )
Run Code Online (Sandbox Code Playgroud)
因此,对于您的用例,您需要使用折线图下的属性marker.color,官方文档中给出了该属性。
color(颜色)
设置标记颜色。它接受特定颜色或数字数组,这些数字映射到相对于数组的最大值和最小值或相对于和(cmin如果cmax设置)的色标。
在这里阅读更多内容
下面是一个简单的工作示例,演示了您的用例,请将其应用于您的解决方案,并让我知道您的问题是否得到解决。
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')
Run Code Online (Sandbox Code Playgroud)
输出: