如何根据第三列绘制具有不同颜色的线图? (KeyError:0)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何根据第三列绘制具有不同颜色的线图? (KeyError:0)相关的知识,希望对你有一定的参考价值。
我有一个数据框
Company Id ON/OFF Level of Fuel
DateTime
2018-08-18 00:00:10 25750275 1 82.048
2018-08-18 00:00:39 25750275 1 82.048
2018-08-18 00:01:09 25750275 1 79.936
2018-08-18 00:01:39 25750275 1 79.600
2018-08-18 00:02:10 25750275 1 78.480
[我想要的是在x轴上具有DateTime索引,在y轴上具有“公司ID”的线图,并且该线应根据[ON?OFF]列中的值[0,1 ]。
从seaborn documentation,我尝试调用以下函数
sns.catplot(x=data.index, y="Level of Fuel", hue="ON/OFF",
palette="ON": 1, "OFF": 0,
markers=["^", "o"], linestyles=["-", "--"],
kind="point",data=data)
但是我得到的KeyError:0
的踪迹如下
KeyError Traceback (most recent call last)
<ipython-input-91-cf07ea2f9cbf> in <module>
2 palette="ON": 1, "OFF": 0,
3 markers=["^", "o"], linestyles=["-", "--"],
----> 4 kind="point",data=data)
~/.local/lib/python3.6/site-packages/seaborn/categorical.py in catplot(x, y, hue, data, row, col, col_wrap, estimator, ci, n_boot, units, seed, order, hue_order, row_order, col_order, kind, height, aspect, orient, color, palette, legend, legend_out, sharex, sharey, margin_titles, facet_kws, **kwargs)
3729 # so we need to define ``palette`` to get default behavior for the
3730 # categorical functions
-> 3731 p.establish_colors(color, palette, 1)
3732 if kind != "point" or hue is not None:
3733 palette = p.colors
~/.local/lib/python3.6/site-packages/seaborn/categorical.py in establish_colors(self, color, palette, saturation)
301 else:
302 levels = self.hue_names
--> 303 palette = [palette[l] for l in levels]
304
305 colors = color_palette(palette, n_colors)
~/.local/lib/python3.6/site-packages/seaborn/categorical.py in <listcomp>(.0)
301 else:
302 levels = self.hue_names
--> 303 palette = [palette[l] for l in levels]
304
305 colors = color_palette(palette, n_colors)
KeyError: 0
答案
如果! Keyerrors显示为“ palette”,您可能以错误的方式构建了字典:零不是键而是值,因此它是 palette=0 : "OFF", 1 : "ON"
以上是关于如何根据第三列绘制具有不同颜色的线图? (KeyError:0)的主要内容,如果未能解决你的问题,请参考以下文章