带回归线和 类 色调的联合图
Joint plot with regression line and classes by hue
我有以下数据框
df = pd.DataFrame({
'Product': ['AA', 'AA', 'BB', 'BB', 'AA', 'AA', 'BB', 'BB'],
'Sales': [ 200, 100, 400, 100, 300, 100, 200, 500],
'Price': [ 5, 3, 3, 6, 4, 7, 4, 1]})
我想绘制整体数据的回归线,但也想在同一图表中按色调(在本例中按产品)绘制散点。
我可以通过以下方式得到回归线:
g = sns.jointplot(y='Sales', x='Price', data=df, kind='reg', scatter = False)
我可以通过以下方式得到散点图:
g = sns.scatterplot(y='Sales', x='Price', data=df, hue='Product')
但是有两个不同的图表。不管怎样,我可以结合这两个命令吗?
您必须告诉散点图您要绘制哪个轴对象。 seaborn jointplot 的选项是主要地块区域 ax_joint
或两个次要地块区域 ax_marg_x
和 ax_marg_y
.
from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd
df = pd.DataFrame({
'Product': ['AA', 'AA', 'BB', 'BB', 'AA', 'AA', 'BB', 'BB'],
'Sales': [ 200, 100, 400, 100, 300, 100, 200, 500],
'Price': [ 5, 3, 3, 6, 4, 7, 4, 1]})
g = sns.jointplot(y='Sales', x='Price', data=df, kind='reg', scatter = False)
sns.scatterplot(y='Sales', x='Price', data=df, hue="Product", ax=g.ax_joint)
plt.show()
示例输出:
为了扩展 T 先生的回答,您也可以这样做以保留联合图的 kde 边际图:
from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd
df = pd.DataFrame({
'Product': ['AA', 'AA', 'BB', 'BB', 'AA', 'AA', 'BB', 'BB'],
'Sales': [ 200, 100, 400, 100, 300, 100, 200, 500],
'Price': [ 5, 3, 3, 6, 4, 7, 4, 1]})
g = sns.jointplot(y='Sales', x='Price', data=df, hue="Product", alpha=0.5, xlim=(0.5,7.5), ylim=(-50, 550))
g1 = sns.regplot(y='Sales', x='Price', data=df, scatter=False, ax=g.ax_joint)
regline = g1.get_lines()[0]
regline.set_color('red')
regline.set_zorder(5)
plt.show()
我有以下数据框
df = pd.DataFrame({
'Product': ['AA', 'AA', 'BB', 'BB', 'AA', 'AA', 'BB', 'BB'],
'Sales': [ 200, 100, 400, 100, 300, 100, 200, 500],
'Price': [ 5, 3, 3, 6, 4, 7, 4, 1]})
我想绘制整体数据的回归线,但也想在同一图表中按色调(在本例中按产品)绘制散点。
我可以通过以下方式得到回归线:
g = sns.jointplot(y='Sales', x='Price', data=df, kind='reg', scatter = False)
我可以通过以下方式得到散点图:
g = sns.scatterplot(y='Sales', x='Price', data=df, hue='Product')
但是有两个不同的图表。不管怎样,我可以结合这两个命令吗?
您必须告诉散点图您要绘制哪个轴对象。 seaborn jointplot 的选项是主要地块区域 ax_joint
或两个次要地块区域 ax_marg_x
和 ax_marg_y
.
from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd
df = pd.DataFrame({
'Product': ['AA', 'AA', 'BB', 'BB', 'AA', 'AA', 'BB', 'BB'],
'Sales': [ 200, 100, 400, 100, 300, 100, 200, 500],
'Price': [ 5, 3, 3, 6, 4, 7, 4, 1]})
g = sns.jointplot(y='Sales', x='Price', data=df, kind='reg', scatter = False)
sns.scatterplot(y='Sales', x='Price', data=df, hue="Product", ax=g.ax_joint)
plt.show()
示例输出:
为了扩展 T 先生的回答,您也可以这样做以保留联合图的 kde 边际图:
from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd
df = pd.DataFrame({
'Product': ['AA', 'AA', 'BB', 'BB', 'AA', 'AA', 'BB', 'BB'],
'Sales': [ 200, 100, 400, 100, 300, 100, 200, 500],
'Price': [ 5, 3, 3, 6, 4, 7, 4, 1]})
g = sns.jointplot(y='Sales', x='Price', data=df, hue="Product", alpha=0.5, xlim=(0.5,7.5), ylim=(-50, 550))
g1 = sns.regplot(y='Sales', x='Price', data=df, scatter=False, ax=g.ax_joint)
regline = g1.get_lines()[0]
regline.set_color('red')
regline.set_zorder(5)
plt.show()