pandas seaborn/matplotlib 中的散点图,点大小和颜色由连续数据框列给出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42754458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
scatter plots in seaborn/matplotlib with point size and color given by continuous dataframe column
提问by jll
I would like to make a scatter plot in seaborn/matplotlib where the size of points is determined by a (continuous) value in a dataframe, and the color of points is also determined by the continuous value of another column in dataframe. In ggplot, the way to do it is:
我想在 seaborn/matplotlib 中制作散点图,其中点的大小由数据框中的(连续)值决定,点的颜色也由数据框中另一列的连续值决定。在ggplot中,这样做的方法是:
ggplot(iris) + geom_point(aes(x=Sepal.Width, y=Sepal.Length, size=Petal.Width, color=Petal.Length))
(color/size here are continuous not categorical values)
what's the syntax for this in seaborn/matplotlib?
seaborn/matplotlib 中的语法是什么?
回答by ImportanceOfBeingErnest
The following reproduces the code diagram from the question. Optaining a legend is a bit cumbersome, because we have to manually define some proxy artists to put to the legend and remove the first automatic legend entry which is generated via the seaborn style.
下面从问题中重现了代码图。选择图例有点麻烦,因为我们必须手动定义一些代理艺术家来放置图例并删除通过 seaborn 样式生成的第一个自动图例条目。
import seaborn as sns
import matplotlib.pyplot as plt
iris = sns.load_dataset("iris")
plt.scatter(iris.sepal_width, iris.sepal_length,
c = iris.petal_length, s=(iris.petal_width**2)*60, cmap="viridis")
ax = plt.gca()
plt.colorbar(label="petal_length")
plt.xlabel("sepal_width")
plt.ylabel("sepal_length")
#make a legend:
pws = [0.5, 1, 1.5, 2., 2.5]
for pw in pws:
plt.scatter([], [], s=(pw**2)*60, c="k",label=str(pw))
h, l = plt.gca().get_legend_handles_labels()
plt.legend(h[1:], l[1:], labelspacing=1.2, title="petal_width", borderpad=1,
frameon=True, framealpha=0.6, edgecolor="k", facecolor="w")
plt.show()
Note that the size argument s
denotes the area of the dots. So in order to have the diameter be proportional to the quantitiy to show, it has to to be squared.
请注意,大小参数s
表示点的面积。因此,为了使直径与要显示的数量成正比,必须对其进行平方。
回答by Nipun Batra
Here is how I would solve this using Altair
这是我将如何使用Altair解决这个问题
from altair import Chart
import seaborn as sns
iris = sns.load_dataset("iris")
c = Chart(iris)
c.mark_circle().encode(
x='sepal_width',
y='sepal_length',
color='petal_length',
size='petal_width',
)
For such plots, Altair might be a great choice. Quoting from their website:
对于这样的地块,Altair 可能是一个不错的选择。引自他们的网站:
Altair is a declarative statistical visualization library for Python, based on Vega-Lite. With Altair, you can spend more time understanding your data and its meaning.
Altair 是一个基于 Vega-Lite 的 Python 声明式统计可视化库。借助 Altair,您可以花更多时间了解数据及其含义。