【发布时间】:2020-07-15 20:39:17
【问题描述】:
我正在尝试使用 pandas 数据框中的所有列使用以下 sn-p 制作意大利面条图:
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'}}
我的情节是这样的:spaghetti plot
我正在使用plotly 4.4.1 和plotly-express 0.4.1。
问题是关键字 repeat 的颜色,所以我很难从图表中做出任何解释。有什么方法可以让我停止重复吗?此外,并非所有关键字都显示在此处,当我保存绘图时标签会被裁剪。我该如何补救? 谢谢!
【问题讨论】:
-
嗨appledora,请发布代码而不是图片。特别是当你分享你的 df.如果能分享
df.head(10).to_dict()就太好了 -
@rpanai 谢谢。字典表格看起来有点乱,所以我认为图像看起来会更干净。不管怎样,我已经改变了描述
-
是的,但是用 dict 比用 pic 更容易重现你的问题。
-
您介意告诉我您使用的是哪个版本的 plotly?只是
plotly.version的输出,它看起来很旧。由于版本4.8它也接受广泛的数据检查here 然后我可以看到你很多列来绘制。我认为我们有超过 10 种不同的痕迹开始重复颜色,除非您自己分配它们。 -
是的,事实证明我使用的是过时的 plotly 版本。
plotly 4.4.1和plotly-express 0.4.1。我会把它添加到问题中,谢谢。
标签: python pandas plotly plotly-express