如何创建一个散点图,其中的标记因变量而异。

huangapple go评论75阅读模式
英文:

How to create a scatter graph with markers that differ by variable

问题

我有一个包含多列数据的数据集:年龄、收入、品牌、产品、收入、支出等等。

我想要在X轴和Y轴上绘制两个变量,年龄和收入,并根据品牌和产品这两个变量的选择来区分标记的样式。

我已经能够使用seaborn的lmplot创建散点图,不同的品牌使用不同的颜色。

目前,我的图上所有的标记都是圆圈,它们的颜色根据它们代表的品牌而不同。例如,品牌A是红色,品牌B是绿色,品牌C是蓝色等等。

是否有一种方式可以根据产品的不同来区分标记的样式?例如,产品1使用X,产品2使用O,产品3使用-。

因此,我的散点图会显示品牌A的产品1为红色X,品牌B的产品3为绿色-。

import pandas as pd
import seaborn as sns

df = pd.read_csv("data.csv")
sns.lmplot(x='age', y='income', height=8, aspect=1, data=df, fit_reg=False, hue='brand', markers=['X', 'o', '-'], legend=True)
英文:

I have a dataset with numerous columns: age, income, brand, product, revenue, expenditure, etc.

I want to plot two of the variables, age and income, against each other on the X and Y-axis, and then I want to differentiate the choice of marker based on two variables, brand and product.

I have been able to do a scatter with different colours for different brands using lmplpt from seaborn.

At the moment, all the markers on my graph are all circles that differ by colour depending on which brand they represent. For example, brand A is red, brand B is green, brand C is blue etc.

Is there a way how the style of the marker will differ depending on the product. For example, product 1 uses an X, product 2 uses a O, product 3 uses -.

Therefore, my scatter plot would show product 1 from brand A as a red X, and product 3 from brand B as a green -.

import pandas as pd
import seaborn as sns

df = pd.read_csv("data.csv")
sns.lmplot(x='age', y='income', height = 8, aspect = 1, data=df, fit_reg=False, hue='brand', legend = True)

答案1

得分: 1

基于所有评论,我相信你想要创建以下内容:

  • 多个散点图,每个国家一个子图
  • 图表应具有形状、颜色、x和y来指示每个点
  • 图例应位于图表区域之外

为此,你需要使用一个图形级别的绘图 - relplot(),并使用 kind="scatter",这实际上是它的默认值。因此,你将需要类似这样的东西。

sns.relplot(data=df, x="age", y="income", hue="brand", style='product', col='country', kind="scatter")

由于我没有你的数据,我使用了企鹅数据集。下面是我的代码和结果图。希望这是你想要的。请注意,使用relplot(),图例是在外面的,所以你不需要使用move_legend()

penguins = sns.load_dataset('penguins')
sns.relplot(data=penguins, x="bill_length_mm", y="bill_depth_mm", hue="species", style='sex', col='island')

图表

如何创建一个散点图,其中的标记因变量而异。

英文:

Based on all the comments, I believe you are looking for creating:

  • Multiple scatter plots, one subplot for each country
  • The plot should have shape, color and x, y to indicate each point
  • Legend should be outside the plot area

For this you will need to use a figure level plot - relplot() with kind="scatter" which is its default value anyway. So, you will need something like this.

sns.relplot(data=df, x="age", y="income", hue="brand", style='product', col='country', kind="scatter")

As I dont have your data, used the penguins dataset. Below is my code and resulting plot. Hope this is what you area looking for. Note that with relplot(), the legend is outside, so you dont need to use move_legend()

penguins=sns.load_dataset('penguins')
sns.relplot(data=penguins, x="bill_length_mm", y="bill_depth_mm", hue="species", style='sex', col='island')

Plot

如何创建一个散点图,其中的标记因变量而异。

huangapple
  • 本文由 发表于 2023年2月19日 18:21:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499421.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定