简体   繁体   English

Plotly:如何增加 colors 的数量以确保所有线路的唯一 colors?

[英]Plotly: How to increase the number of colors to assure unique colors for all lines?

I'd to plot a simple line plot but if I have more than 10 variables, plolty use the same color twice, how can I avoid it and always have a new color for a new variable?我想 plot 一条简单的线 plot 但如果我有超过 10 个变量,plolty 使用相同的颜色两次,我怎样才能避免它并且总是为新变量使用新颜色?

import pandas as pd
import numpy as np
pd.set_option("plotting.backend", "plotly")

df=pd.DataFrame(np.random.rand(100, 12)).cumsum()
df.plot()

Output: Output:

在此处输入图像描述

You can pass a list of colors using the keyword colors like df.plot(colors=my_list) .您可以使用关键字colorsdf.plot(colors=my_list)传递 colors 的列表。

Ich your list has as many colors as your DataFrame columns, the colors aren't repeaded.如果您的列表中的 colors 与您的 DataFrame 列一样多,则不会重复 colors。

Here is a example:这是一个例子:

import pandas as pd
import numpy as np
import colorcet as cc

pd.set_option("plotting.backend", "matplotlib")
df=pd.DataFrame(np.random.rand(100, 12)).cumsum()
df.plot(color=cc.b_rainbow_bgyrm_35_85_c71[::15][:df.shape[0]])

Output Output

使用 12 种颜色

Effectively it's documented here https://plotly.com/python/discrete-color/ .实际上它记录在这里https://plotly.com/python/discrete-color/ You are using interface to plotly express您正在使用plotly express的接口

Code below using a different set of colors.下面的代码使用一组不同的 colors。

import pandas as pd
import numpy as np
pd.set_option("plotting.backend", "plotly")

df=pd.DataFrame(np.random.rand(100, 12)).cumsum()
color_seq = ['#AA0DFE',
 '#3283FE',
 '#85660D',
 '#782AB6',
 '#565656',
 '#1C8356',
 '#16FF32',
 '#F7E1A0',
 '#E2E2E2',
 '#1CBE4F',
 '#C4451C',
 '#DEA0FD',
 '#FE00FA',
 '#325A9B',
 '#FEAF16',
 '#F8A19F',
 '#90AD1C',
 '#F6222E',
 '#1CFFCE',
 '#2ED9FF',
 '#B10DA1',
 '#C075A6',
 '#FC1CBF',
 '#B00068',
 '#FBE426',
 '#FA0087']

df.plot(color_discrete_sequence=color_seq)


If you'd like to do this dynamically with regards to an arbitrary number of traces, you can sample a continuous plotly colorscale using px.colors.sample_colorscale() like this:如果您想针对任意数量的轨迹动态执行此操作,您可以使用px.colors.sample_colorscale()连续的 plotly 色标进行采样,如下所示:

colors = px.colors.sample_colorscale("viridis", [n/(n_colors -1) for n in range(n_colors)])
df.plot(color_discrete_sequence=colors)

Plot 1 - 12 traces Plot 1 - 12 迹线

在此处输入图像描述

Plot 2 - 50 traces Plot 2 - 50 迹线

在此处输入图像描述

Complete code:完整代码:

import pandas as pd
import numpy as np
import plotly.express as px
pd.set_option("plotting.backend", "plotly")

# data
df=pd.DataFrame(np.random.uniform(low=-2, high=2, size=(100,12))).cumsum()

# colors
n_colors = len(df.columns)
colors = px.colors.sample_colorscale("viridis", [n/(n_colors -1) for n in range(n_colors)])

# plot
df.plot(color_discrete_sequence=colors)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 图像中唯一 CIELab colors 的数量 - Number of unique CIELab colors in an image Plotly:如何使用匹配行的文本和标记 colors 注释多行的结尾? - Plotly: How to annotate end of multiple lines with text and marker colors that match the lines? 在 Plotly 中单独显示所有 colors 的线性拟合 - Show Linear Fits for all colors individually in Plotly python matplotlib 如何绘制 20 或所需数量的唯一和不同颜色而不是渐变颜色 - python matplotlib how to plot 20 or required number unique and different colors not gradient colors 在 geoTIFF 中获取所有独特颜色时,如何知道 PIL 指的是什么颜色? - How to know what colors PIL is referring to when getting all unique colors in a geoTIFF? Plotly 快速折线图 - 获取默认 colors(如何按照字典 object 的指定对线条进行着色?) - Plotly express line chart - get default colors (how to color lines as specified by a dictionary object?) Python:如何更改 plotly 中多折线图上的折线 colors? - Python: How do I change the colors of lines on a multi-line chart in plotly? 如何更改旭日图上环的颜色 - How to change the colors of rings on a sunburst Plotly chart Plotly - python - 如何循环通过 colors - Plotly - python - How to loop through colors 如何更改每个圆形扇区的colors? plotly - how to change the colors of each circular sector? plotly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM