Changing Color And Marker Of Each Point Using Seaborn Jointplot
Answer :
Solving this problem is almost no different than that from matplotlib (plotting a scatter plot with different markers and colors), except I wanted to keep the marginal distributions:
import seaborn as sns from itertools import product sns.set(style="darkgrid") tips = sns.load_dataset("tips") color = sns.color_palette()[5] g = sns.jointplot("total_bill", "tip", data=tips, kind="reg", stat_func=None, xlim=(0, 60), ylim=(0, 12), color='k', size=7) #Clear the axes containing the scatter plot g.ax_joint.cla() #Generate some colors and markers colors = np.random.random((len(tips),3)) markers = ['x','o','v','^','<']*100 #Plot each individual point separately for i,row in enumerate(tips.values): g.ax_joint.plot(row[0], row[1], color=colors[i], marker=markers[i]) g.set_axis_labels('total bill', 'tip', fontsize=16)
Which gives me this:
The regression line is now gone, but this is all I needed.
The accepted answer is too complicated. plt.sca()
can be used to do this in a simpler way:
import matplotlib.pyplot as plt import seaborn as sns tips = sns.load_dataset("tips") g = sns.jointplot("total_bill", "tip", data=tips, kind="reg", stat_func=None, xlim=(0, 60), ylim=(0, 12)) g.ax_joint.cla() # or g.ax_joint.collections[0].set_visible(False), as per mwaskom's comment # set the current axis to be the joint plot's axis plt.sca(g.ax_joint) # plt.scatter takes a 'c' keyword for color # you can also pass an array of floats and use the 'cmap' keyword to # convert them into a colormap plt.scatter(tips.total_bill, tips.tip, c=np.random.random((len(tips), 3)))
You can also directly precise it in the list of arguments, thanks to the keyword : joint_kws
(tested with seaborn 0.8.1). If needed, you can also change the properties of the marginal with marginal_kws
So your code becomes :
import seaborn as sns colors = np.random.random((len(tips),3)) markers = (['x','o','v','^','<']*100)[:len(tips)] sns.jointplot("total_bill", "tip", data=tips, kind="reg", joint_kws={"color":colors, "marker":markers})
Comments
Post a Comment