1

我正在尝试使用以下代码段使用 pandas 数据框中的所有列制作意大利面条图:

import pandas as pd
import plotly.express as px

df_melt = df.melt('months', var_name='cols',  value_name='vals')
fig = px.line(df_melt, x='months' , y='vals' , color='cols',title="Symptoms frequency over the years 2018-19", labels={"vals":"Frequency","cols":"Keywords","months":"Timeline"})
fig.update_layout(font = dict(size = 10))

csv 看起来像这样:

{'pathogen': {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}, 
'flavivirus': {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}, 
'swamp': {0: 5, 1: 6, 2: 9, 3: 19, 4: 39}, 
'virus': {0: 0, 1: 3, 2: 1, 3: 1, 4: 8}, 
'virus-bacteria': {0: 11, 1: 12, 2: 18, 3: 21, 4: 97}, 
'contagious': {0: 8, 1: 1, 2: 2, 3: 9, 4: 23}, 
'mosquito bite': {0: 1, 1: 1, 2: 6, 3: 3, 4: 7}, 
'agent': {0: 19, 1: 17, 2: 43, 3: 179, 4: 702}, 
'host': {0: 1, 1: 1, 2: 1, 3: 3, 4: 27}, 
'infected': {0: 0, 1: 1, 2: 0, 3: 1, 4: 4}, 
'bitten': {0: 0, 1: 1, 2: 0, 3: 2, 4: 12}, 
'bite': {0: 7, 1: 9, 2: 17, 3: 39, 4: 93}, 
'skin': {0: 34, 1: 43, 2: 38, 3: 154, 4: 269}, 
'sensitive': {0: 9, 1: 2, 2: 3, 3: 40, 4: 75}, 
'at risk': {0: 20, 1: 23, 2: 34, 3: 138, 4: 338}, 
'serotype': {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}, 
'serotypes': {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}, 
'protection': {0: 37, 1: 36, 2: 50, 3: 259, 4: 303}, 
'mucus membrane': {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}, 
'spreaded': {0: 172, 1: 194, 2: 266, 3: 825, 4: 1424}, 
'vertebrate': {0: 0, 1: 0, 2: 0, 3: 0, 4: 1}, 
'human': {0: 707, 1: 676, 2: 961, 3: 2623, 4: 5270}, 
'mosquito': {0: 2, 1: 3, 2: 12, 3: 59, 4: 41}, 
'DEN-1': {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}, 
'den1': {0: 0, 1: 2, 2: 1, 3: 0, 4: 3}, 
'DEN-2': {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}, 
'den2': {0: 0, 1: 3, 2: 1, 3: 0, 4: 1}, 
'DEN-3': {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}, 
'den3': {0: 0, 1: 1, 2: 0, 3: 0, 4: 2}, 
'DEN-4': {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}, 
'den4': {0: 0, 1: 3, 2: 0, 3: 1, 4: 3}, 
'DEN': {0: 1, 1: 4, 2: 2, 3: 4, 4: 5}, 
'months': {0: 'January18', 1: 'February18', 2: 'March18', 3: 'April18', 4: 'May18'}}

我的情节看起来像这样:意大利面条情节

我正在使用plotly 4.4.1plotly-express 0.4.1

问题是关键字 repeat 的颜色,所以我很难从图表中做出任何解释。有什么方法可以让我停止重复吗?此外,并非所有关键字都显示在此处,当我保存绘图时标签会被裁剪。我该如何补救?谢谢!

4

1 回答 1

0

默认的定性色阶只有 10 项,如您键入:

print(len(px.colors.qualitative.Plotly))

10

要使用不同的比例,您可以使用 plotly expresscolor_discrete_sequence参数。例如,要使用该Dark24方案(该方案有 24 种颜色),您可以运行:

px.line(..., color_discrete_sequence = px.colors.qualitative.Dark24)

如果你传递一个列表,你也可以传递你自己的配色方案:

px.line(..., color_discrete_sequence = ['#FF0000', ...])

您也可以将新颜色附加到默认配色方案中:

mycolorscheme = px.colors.qualitative.Plotly
mycolorscheme.append('#FF0000')
px.line(..., color_discrete_sequence = mycolorscheme)

您可以在以下链接中查看有关颜色的更多信息:

于 2020-07-16T12:50:12.517 回答