[英]How to add x and y axis line in seaborn scatter plot
I used the following code to create scatterplot (data is imported as an example).我使用以下代码创建散点图(数据以导入为例)。 However, the plot was created without x and y axis, which looks weird.
但是,plot 是在没有 x 和 y 轴的情况下创建的,这看起来很奇怪。 I would like to keep facecolor='white' as well.
我也想保留 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'
);
Any suggestions?有什么建议么? Thanks a lot.
非常感谢。
You seem to have explicitly set the default seaborn theme.您似乎已经明确设置了默认的 seaborn 主题。 That has no border (so also no line for x and y axis), a grey facecolor and white grid lines.
它没有边框(x 和 y 轴也没有线),灰色的 facecolor 和白色的网格线。 You can use
sns.set_style("whitegrid")
to have a white facecolor.您可以使用
sns.set_style("whitegrid")
获得白色的 facecolor。 You can also use sns.despine()
to only show the x and y-axis but no "spines" at the top and right.您也可以使用
sns.despine()
仅显示 x 轴和 y 轴,但在顶部和右侧不显示“脊椎”。 See Controlling figure aesthetics for more information about fine-tuning how the plot looks like.有关微调 plot 外观的更多信息,请参阅控制图形美学。
Here is a comparison.这是一个比较。 Note that the style should be set before the axes are created, so for demo-purposes
plt.subplot
creates the axes one at a time.请注意,应在创建轴之前设置样式,因此出于演示目的
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.