[英]Using Seaborn to connect corresponding x and y events on a scatter plot with a line
[英]How to add x and y axis line in seaborn scatter plot
我使用以下代码创建散点图(数据以导入为例)。 但是,plot 是在没有 x 和 y 轴的情况下创建的,这看起来很奇怪。 我也想保留 facecolor='white' 。
import seaborn as sns
tips = sns.load_dataset("tips")
fig, ax = plt.subplots(figsize=(10, 8))
sns.scatterplot(
x='total_bill',
y='tip',
data=tips,
hue='total_bill',
edgecolor='black',
palette='rocket_r',
linewidth=0.5,
ax=ax
)
ax.set(
title='title',
xlabel='total_bill',
ylabel='tip',
facecolor='white'
);
有什么建议么? 非常感谢。
您似乎已经明确设置了默认的 seaborn 主题。 它没有边框(x 和 y 轴也没有线),灰色的 facecolor 和白色的网格线。 您可以使用sns.set_style("whitegrid")
获得白色的 facecolor。 您也可以使用sns.despine()
仅显示 x 轴和 y 轴,但在顶部和右侧不显示“脊椎”。 有关微调 plot 外观的更多信息,请参阅控制图形美学。
这是一个比较。 请注意,应在创建轴之前设置样式,因此出于演示目的plt.subplot
创建一个轴。
import matplotlib.pyplot as plt
import seaborn as sns
sns.set() # set the default style
# sns.set_style('white')
tips = sns.load_dataset("tips")
fig = plt.figure(figsize=(18, 6))
for subplot_ind in (1, 2, 3):
if subplot_ind >= 2:
sns.set_style('white')
ax = plt.subplot(1, 3, subplot_ind)
sns.scatterplot(
x='total_bill',
y='tip',
data=tips,
hue='total_bill',
edgecolor='black',
palette='rocket_r',
linewidth=0.5,
ax=ax
)
ax.set(
title={1: 'Default theme', 2: 'White style', 3: 'White style with despine'}[subplot_ind],
xlabel='total_bill',
ylabel='tip'
)
if subplot_ind == 3:
sns.despine(ax=ax)
plt.tight_layout()
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.