0

I have a dataframe

                       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     

What I want is a line plot with DateTime index in x-axis, "Company Id" in y-axis and the line should be differently coloured or styled according to value in "ON?OFF" column which is [0,1].

From seaborn documentation, i tried calling the following function

sns.catplot(x=data.index, y="Level of Fuel", hue="ON/OFF",
            palette={"ON": 1, "OFF": 0},
            markers=["^", "o"], linestyles=["-", "--"],
            kind="point",data=data)

But I am getting a KeyError:0 whose trace is as follows

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

raaj
  • 403
  • 1
  • 5
  • 17

1 Answers1

0

IF! the Keyerrors appears with "palette" you might have build your dict the wrong way round: Zero is not a key but a value, so it would be

 palette={0 :  "OFF", 1 : "ON"}
Qsnok
  • 43
  • 5