使用 Seaborn 的 FacetGrid 时如何为所有绘图添加比较线
How to add a comparison line to all plots when using Seaborn's FacetGrid
我正在尝试使用 FacetGrid 将相同的比较线添加到多个图中。这是我卡住的地方:
# Import the dataset
tips = sns.load_dataset("tips")
# Plot using FaceGrid, separated by smoke
g = sns.FacetGrid(tips, col="smoker", size=5, aspect=1.5)
g.map(plt.scatter, "tip", "total_bill")
x = np.arange(0, 50, .5)
y = 0.2*x
plt.plot(y, x, C='k')
plt.show()
Here are the results
如您所见,该线出现在最后一个图上,但没有出现在第一个图上。我如何在两者上都得到它?
您可以 map
与 FacetGrid
相同的功能。
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# Import the dataset
tips = sns.load_dataset("tips")
# Plot using FaceGrid, separated by smoke
g = sns.FacetGrid(tips, col="smoker", height=5, aspect=1.5)
g.map(plt.scatter, "tip", "total_bill")
def const_line(*args, **kwargs):
x = np.arange(0, 50, .5)
y = 0.2*x
plt.plot(y, x, C='k')
g.map(const_line)
plt.show()
另一种间接方法是从 FacetGrid
中获取 axes
对象,然后绘制到每个对象的直线
g = sns.FacetGrid(tips, col="smoker", size=5, aspect=1.5)
g.map(plt.scatter, "tip", "total_bill")
axes = g.fig.axes
x = np.arange(0, 50, .5)
y = 0.2*x
for ax in axes:
ax.plot(y, x, C='k')
plt.show()
我正在尝试使用 FacetGrid 将相同的比较线添加到多个图中。这是我卡住的地方:
# Import the dataset
tips = sns.load_dataset("tips")
# Plot using FaceGrid, separated by smoke
g = sns.FacetGrid(tips, col="smoker", size=5, aspect=1.5)
g.map(plt.scatter, "tip", "total_bill")
x = np.arange(0, 50, .5)
y = 0.2*x
plt.plot(y, x, C='k')
plt.show()
Here are the results
如您所见,该线出现在最后一个图上,但没有出现在第一个图上。我如何在两者上都得到它?
您可以 map
与 FacetGrid
相同的功能。
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# Import the dataset
tips = sns.load_dataset("tips")
# Plot using FaceGrid, separated by smoke
g = sns.FacetGrid(tips, col="smoker", height=5, aspect=1.5)
g.map(plt.scatter, "tip", "total_bill")
def const_line(*args, **kwargs):
x = np.arange(0, 50, .5)
y = 0.2*x
plt.plot(y, x, C='k')
g.map(const_line)
plt.show()
另一种间接方法是从 FacetGrid
中获取 axes
对象,然后绘制到每个对象的直线
g = sns.FacetGrid(tips, col="smoker", size=5, aspect=1.5)
g.map(plt.scatter, "tip", "total_bill")
axes = g.fig.axes
x = np.arange(0, 50, .5)
y = 0.2*x
for ax in axes:
ax.plot(y, x, C='k')
plt.show()