5

I have a pandas dataframe, with columns 'groupname', 'result', and 'temperature'. I've plotted a Seaborn swarmplot, where x='groupname' and y='result', which shows the results data separated into the groups.

What I also want to do is to colour the markers according to their temperature, using a colormap, so that for example the coldest are blue and hottest red.

Plotting the chart is very simple:

import pandas as pd
import seaborn as sns
import matplotlib
import matplotlib.pyplot as plt

data = {'groupname': ['G0', 'G0', 'G0', 'G0', 'G1', 'G1', 'G1'], 'shot': [1, 2, 3, 4, 1, 2, 3], 'temperature': [20, 25, 35, 10, -20, -17, -6], 'result': [10.0, 10.1, 10.5, 15.0, 15.1, 13.5, 10.5]}
df = pd.DataFrame(data)

  groupname  shot  temperature  result
0        G0     1           20    10.0
1        G0     2           25    10.1
2        G0     3           35    10.5
3        G0     4           10    15.0
4        G1     1          -20    15.1
5        G1     2          -17    13.5
6        G1     3           -6    10.5

plt.figure()
sns.stripplot(data=results, x="groupname", y="result")
plt.show()

But now I'm stuck trying to colour the points, I've tried a few things like:

sns.stripplot(data=results, x="groupname", y="result", cmap=matplotlib.cm.get_cmap('Spectral'))

which doesn't seem to do anything.

Also tried:

sns.stripplot(data=results, x="groupname", y="result", hue='temperature')

which does colour the points depending on the temperature, however the colours are random rather than mapped.

I feel like there is probably a very simple way to do this, but haven't been able to find any examples.

Ideally looking for something like:

sns.stripplot(data=results, x="groupname", y="result", colorscale='temperature')
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Henry Gibson
  • 192
  • 1
  • 8
  • 1
    As far as I understand, `seaborn` expects the variable passed to `hue` to be categorical rather than continuous. See [this question and its answers](https://stackoverflow.com/questions/44641669/scatterplot-with-point-colors-representing-a-continuous-variable-in-seaborn-face) for more information on using color maps in `seaborn` plots. – Brendan Jul 13 '19 at 16:31

1 Answers1

3

Hello the keyword you are looking for is "palette"

Below should work:

sns.stripplot(data=results, x="groupname", y="result", hue='temperature',palette="vlag")

http://man.hubwiz.com/docset/Seaborn.docset/Contents/Resources/Documents/generated/seaborn.stripplot.html

Ivo Leist
  • 408
  • 3
  • 12
  • 2
    While true, this only works for small numbers of `temperature`. If `temperature` takes on many values, this becomes problematic. – Brendan Jul 13 '19 at 16:30
  • 1
    True, I run in that problems in the past as well...my workaround for that is to bin the values into ranges and take these range categories as hue. Do you have a better idea @BrendanCox ? – Ivo Leist Jul 13 '19 at 16:38
  • 1
    I think that is what I'll do. The data I'm working with will roughly be at three temperatures so I will group them, the precise values are not important. Would have been nice if there was a straightforward way to automatically colour based on temp value, but it's not actually essential for what I'm doing. – Henry Gibson Jul 13 '19 at 19:01