Seaborn似乎对显示的轴标签做了很多修改,因为我不能在上面覆盖“常规”matplotlib对象。我如何修复下面的行为?
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
fig, ax = plt.subplots(figsize=(10, 6))
data = pd.DataFrame()
data["value"] = np.random.normal(0, 1, 1000)
data["week"] = np.random.randint(20, 30, 1000)
# make a violin plot, and put a line on top of it
sns.violinplot(data=data, x="week", y="value", scale="width", linewidth=0.5, palette="viridis")
# fit a line to the data
x = data["week"].values
y = data["value"].values
m, b = np.polyfit(x, y, 1)
y_hat = m * x + b
# plot the line
ax.plot(x, y_hat, color="black", linewidth=2)
1条答案
按热度按时间68de4m5k1#
这是因为
violinplot
将x
视为Categorical。关于doc:此函数始终将其中一个变量视为分类变量,并在顺序位置(0,1,...)绘制数据。n),即使数据为数字或日期类型。
如果你在x线上减去20,你会得到小提琴图上的线(你可能也想改变y):