Skip to content
Advertisement

Seaborn scatter plot from pandas dataframe colours based on third column

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')

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement