【问题标题】:How to plot line plot with different colours based on third column? (KeyError:0)如何根据第三列绘制不同颜色的线图? (关键错误:0)
【发布时间】:2020-05-14 05:49:48
【问题描述】:

我有一个数据框

                       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

【问题讨论】:

    标签: python matplotlib seaborn keyerror catplot


    【解决方案1】:

    如果! Keyerrors 出现在“调色板”中,您可能以错误的方式构建了您的 dict:零不是键而是值,所以它会是

     palette={0 :  "OFF", 1 : "ON"}
    

    【讨论】:

    • 我收到了ValueError: Could not generate a palette for &lt;map object at 0x7faa1c27c400&gt;
    猜你喜欢
    • 2015-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 2016-11-18
    • 1970-01-01
    • 2011-01-04
    相关资源
    最近更新 更多