Scatter plot with variable marker size (seaborn)

橙三吉。 提交于 2019-12-05 01:34:21

问题


I am using a seaborn pairplot to plot a scatter plot of different dimensions of my datapoints. However, I want the markers of the datapoints to have a size that corresponds to one of the dimensions of the datapoints. I have the following code:

markersize = 1000* my_dataframe['dim_size'] / sum(my_dataframe['dim_size'])

sns.set_context("notebook", font_scale=1.5, rc={'figure.figsize': [11, 8]})
sns.set_style("darkgrid", {"axes.facecolor": ".9"})

kws = dict(s=markersize, linewidth=.5, edgecolor="w")

sbax = sns.pairplot(my_dataframe, hue='dim_hue' x_vars=['dim_1', 'dim_2'], y_vars=['dim_3', 'dim_4'], size=5, plot_kws=kws)

axes = sbax.axes
for a in axes.flatten():
    a.set_ylim([0,1])
    a.set_xlim([0,1])

If I do print(kws), I see in the dictionary that the sizes are all different and vary from 40 to 2000. However, the markers on the plot are all the same. Is there any way to achieve what I want?

Btw, this works very well with lmplot if I set the parameter scatter_kws={"s": markersize}.

Thanks!


回答1:


import seaborn as sns
import matplotlib.pyplot as plt
iris = sns.load_dataset("iris")

size = 100 * (iris.petal_length / iris.petal_length.max())
g = sns.PairGrid(iris, vars=["sepal_length", "sepal_width"], size=5)
g.map(plt.scatter, s=size)



来源:https://stackoverflow.com/questions/35630749/scatter-plot-with-variable-marker-size-seaborn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!