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')
1
  • 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 for more information on using color maps in seaborn plots. Commented Jul 13, 2019 at 16:31

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

While true, this only works for small numbers of temperature. If temperature takes on many values, this becomes problematic.
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 ?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.