我正在尝试使用plotly和破折号为Web应用程序创建简单的折线图。我想要一条连接两点的线。我希望其中一个成为红色,另一个成为绿色。这是我到目前为止的内容:
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets = external_stylesheets)
graph = dcc.Graph(figure = {
'data' : [
go.Scatter(x = [1,4], y = [2,3], mode = 'lines+markers', opacity = 0.7, marker = {
'size' : 15,
'line' : {'width' : 0.5, 'color' : 'black'}
})
]
})
app.layout = html.Div([graph])
if __name__ == '__main__':
app.run_server(debug = False)
我正在使用所有最新软件包的Jupyter笔记本中运行此代码。 如果运行此代码,则会得到所需的线图,但是两个点都是蓝色的。我希望对应于(1,2)的点为红色,而(4,3)的点为绿色。我该怎么做呢? 提前非常感谢您!
答案 0 :(得分:2)
我相信,此问题的公认解决方案是在图形数据中包含3个不同的go.scatter()
对象。一个用于行,一个用于每个标记。您的应用最终看起来像这样:
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets = external_stylesheets)
graph = dcc.Graph(figure = {
'data' : [
go.Scatter(x = [1,4], y = [2,3], mode = 'lines+markers', opacity = 0.7,
marker={'color':["red","green"], "size":15})
]
})
app.layout = html.Div([graph])
if __name__ == '__main__':
app.run_server(debug = False)