[英]Two line-plots with the same color in plotly
I am quiet new to plotly
and usually use matplotlib
.我对
plotly
很安静,通常使用matplotlib
。 However, I have to use plotly
this time (due to dash
).但是,这次我必须使用
plotly
(由于dash
)。 What I want is very simple with matplotlib
, but I don't get it working with plotly
: Two lines in the same figure that have the same color.我想要的
matplotlib
非常简单,但我不明白它与plotly
一起使用:同一图中的两条线具有相同的颜色。
With matplotlib
, this would look as follows:使用
matplotlib
,这将如下所示:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([0, 1, 2, 4], [0, 1, 2, 3], color='black')
ax.plot([0, 1, 2, 4], [10, 9, 8, 7], color='black')
However, I have a hard time controlling the color in plotly
.但是,我很难控制
plotly
中的颜色。 How do I get the above result with plotly
?如何使用
plotly
获得上述结果? I have tried a kind-of workaround, which gives me two lines with the same color, but not the color I want (which is black):我尝试了一种解决方法,它给了我两条颜色相同的线,但不是我想要的颜色(黑色):
import numpy as np
import pandas as pd
import plotly.express as px
df = pd.DataFrame({'x': [0, 1, 2, 4], 'y1': [0, 1, 2, 3], 'y2': [10, 9, 8, 7]})
fig = px.line(df, x='x', y=['y1', 'y2'], color=np.ones(len(df))
The result should look something like this:结果应如下所示:
I understand that it is normally "bad practice" to use the same color for two lines.我知道为两条线使用相同的颜色通常是“不好的做法”。 However, in this specific case it is not;
但是,在这种特定情况下并非如此; I want to plot some self-defined boundaries that function as a reference for other data that is to be added to the figure.
我想 plot 一些自定义边界,即 function 作为要添加到图中的其他数据的参考。
EXTRA: These lines should not be included in the legend.额外:这些行不应包含在图例中。 However, I want to plot 2D data in between these lines for which I would like to use some color-coding.
但是,我想在这些行之间使用 plot 2D 数据,我想对其使用一些颜色编码。 This color-coding is preferably in the legend/added as a colorbar.
这种颜色编码最好在图例中/作为颜色条添加。
Thank you very much for your help!非常感谢您的帮助!
Since Plotly documentation does not describe desired output, try avoiding color
variable.由于 Plotly文档没有描述所需的 output,请尽量避免
color
变量。
For instance:例如:
import plotly.express as px
df = px.data.gapminder().query("continent=='Oceania'")
fig = px.line(df, x="year", y="lifeExp")
fig.show()
Gives this output:给出这个 output:
update_traces()
update_traces()
覆盖分配的颜色import numpy as np
import pandas as pd
import plotly.express as px
df = pd.DataFrame({'x': [0, 1, 2, 4], 'y1': [0, 1, 2, 3], 'y2': [10, 9, 8, 7]})
fig = px.line(df, x='x', y=['y1', 'y2']).update_traces(line_color="black", showlegend=False)
fig
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.