gpt4 book ai didi

python - Plotly:如何为每个变量创建具有不同样式和颜色的线图?

转载 作者:行者123 更新时间:2023-12-02 16:24:08 25 4
gpt4 key购买 nike

我正在尝试使用 Plotly Express 创建一个包含 10 条不同颜色和标记的不同线条的图表。类似这样的东西:

enter image description here

我可以按照文档中的建议使用 px.line 函数创建具有不同颜色的漂亮图表。我的代码如下所示:

import plotly.express as px
import numpy as np
import pandas as pd

rand_elems = []
for i in range(10):
rand_elems.append(np.random.randn(25))

data = pd.DataFrame(rand_elems)

px.line(data_frame=data.T)

我的折线图是这样的:

enter image description here

其中每个变量都是一个 (25,) numpy 数组,其中包含来自标准正态分布的随机值(使用 np.random.randn(25) 创建)。

有没有一种方法可以为每一行添加不同的样式?也欢迎使用其他绘图库,因为我在 Plotly 的文档中找不到解决方案。

我知道我可以使用的线型有限。也许我可以循环浏览它们和颜色?对此有什么好的解决方案?

编辑:图表的目的仅仅是为了表明信号是随机的并且在标准正态分布限制内。

最佳答案

我采用了@veSTLand 的代码并进行了调整并修复为一个更简单的版本,它完全满足了我的需要。这个想法是使用 plotly.validators.scatter.marker 中的 SymbolValidator(),如@veSTLand 的回答中所述。还可以向此列表中添加一个随机因素以获得更不同的结果。

示例运行代码:

import plotly.express as px
from itertools import cycle
from plotly.validators.scatter.marker import SymbolValidator

# data
df = px.data.gapminder()
df = df[df['country'].isin(['Canada', 'USA', 'Norway', 'Sweden', 'Germany'])]

# plotly
fig = px.line(df, x='year', y='lifeExp',
color='country',
line_dash = 'continent')

raw_symbols = SymbolValidator().values
# Take only the string values which are in this order.
symbols_names = raw_symbols[::-2]
markers = cycle(symbols_names)

# set unique marker style for different countries
fig.update_traces(mode='lines+markers')
for d in fig.data:
d.marker.symbol = next(markers)
d.marker.size = 10
fig.show()

此代码生成以下图表: enter image description here非常感谢@veSTLand 的见解,我想知道 Plotly 是否可以在下一个版本中将其作为参数选项。

更新:

如果您的 DataFrame 没有像我的那样用于 line_dash 参数的聚合列,您也可以循环浏览具有线条样式的列表并使用 `fig.data.line["覆盖它们dash"] 如下所示:

  • 生成数据和导入包的代码。
import numpy as np
import random
import plotly.express as px
import numpy as np
import pandas as pd
from itertools import cycle
from random import shuffle
from plotly.validators.scatter.marker import SymbolValidator
from plotly.validators.scatter.line import DashValidator

rand_elems = []
for i in range(10):
rand_elems.append(np.random.randn(25))

data = pd.DataFrame(rand_elems)
data.index = [f"Signal {i}" for i in range(10)]
  • 为 DataFrame 中的每一列生成不同标记和线条样式的代码。
line_styles_names = ['solid', 'dot', 'dash', 'longdash', 'dashdot', 'longdashdot']
line_styles = cycle(line_styles_names)

fig = (px.line(data_frame=data.T,
color_discrete_sequence=px.colors.qualitative.Bold_r,
title="First 10 Signals Visualization",
))

symbols_names = list(set([i.replace("-open", "").replace("-dot", "") for i in SymbolValidator().values[::-2]]))
shuffle(symbols_names)
markers = cycle(symbols_names)

_ = fig.update_traces(mode='lines+markers')
for d in fig.data:
d.line["dash"] = next(line_styles)
d.marker.symbol = next(markers)
d.marker.size = 10
fig.show()

有了这个,结果应该类似于这个,这正是我想要的。 enter image description here

关于python - Plotly:如何为每个变量创建具有不同样式和颜色的线图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64879666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com