Issue
I currently have a xy graph color being my third variable. Is it possible to make a 4th variable be represented by shape or size?
I've been trying to use shape to represent my 4th variable, but I am starting to doubt whether it's possible.
In the code below, is it possible to make the 'marker' parameter reflect the value of the 'Species' variable?
iris = sns.load_dataset('iris')
fig, ax = plt.subplots()
sc = ax.scatter(data=iris, x='petal_length',
y='petal_width', c='sepal_length', cmap='coolwarm')
Solution
Yes. But before we start, I'm not clear if you're mixing things or not. I'll explain it both ways.
Option 1, with iris = sns.load_dataset('iris')
you're calling seaborn, which you've presumably already imported. Seaborn is built on top of matplotlib, so it's quite possible that what you really wanted was to do this in seaborn because it looks nice. If that's the case, you'd need a different set of commands and relplot would be a good option.
sns.relplot(data=iris, x='petal_length', y='petal_width', size='petal_length', hue="species")
Which would display this:
Option 2 is along the lines of using seaborn just for a quick set of data to plot but really wanting to stick with matplotlib for some reason.
For that, you need to set what indicates what the sizes are initially. You can then change those size in the paramaters.
fig, ax = plt.subplots()
s = iris['petal_length']
sc = ax.scatter(data=iris, s=s*10, x='petal_length', y='petal_width',
c='sepal_length', cmap='coolwarm')
Which would display this:
Answered By - hrokr
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.