sns.pairplot设置x轴、y轴、图例的文字大小
时间: 2023-08-13 14:03:08 浏览: 277
sns.pairplot() 函数可以通过设置参数来调整 x 轴、y 轴和图例的文字大小。其中,字体大小的参数为 font。
下面是一个例子:
```python
import seaborn as sns
import matplotlib.pyplot as plt
# 加载数据集
iris = sns.load_dataset("iris")
# 设置 seaborn 样式
sns.set(style="ticks", font_scale=1.2)
# 绘制 pairplot
g = sns.pairplot(iris, hue="species")
# 设置 x 轴、y 轴和图例的字体大小
g.set_axis_labels(fontsize=14)
g._legend.set_title(fontsize=16)
g._legend.fontsize = 14
plt.show()
```
在这个例子中,我们使用了 Seaborn 的 iris 数据集,然后调用 sns.pairplot() 函数绘制散点图矩阵。通过设置 font_scale 参数来调整字体大小,然后使用 set_axis_labels() 函数来设置 x 轴、y 轴的字体大小,使用 _legend.set_title() 和 _legend.fontsize 来设置图例的标题和字体大小。最后,使用 plt.show() 函数显示图像。
相关问题
用seaborn绘制堆积柱状图和折线图到一张画布上,堆积柱状图和折线图分别使用不同的Y轴,都有图例
以下是一个示例代码,其中使用了Seaborn中的`barplot`和`lineplot`绘制堆积柱状图和折线图,使用了`twinx()`函数创建另一个Y轴,并使用`legend()`函数添加图例:
```python
import seaborn as sns
import matplotlib.pyplot as plt
# 创建数据
tips = sns.load_dataset('tips')
grouped = tips.groupby(['day', 'time']).agg({'total_bill': 'mean', 'tip': 'mean'}).reset_index()
# 创建画布和子图
fig, ax1 = plt.subplots(figsize=(10,6))
# 绘制堆积柱状图
sns.barplot(data=grouped, x='day', y='total_bill', hue='time', ax=ax1)
# 创建第二个Y轴并绘制折线图
ax2 = ax1.twinx()
sns.lineplot(data=grouped, x='day', y='tip', hue='time', ax=ax2, sort=False)
# 添加图例
ax1.legend(loc='upper left', bbox_to_anchor=(1,1))
ax2.legend(loc='upper right', bbox_to_anchor=(1,0.9))
# 设置坐标轴标签和标题
ax1.set_xlabel('Day')
ax1.set_ylabel('Total Bill')
ax2.set_ylabel('Tip')
plt.title('Total Bill and Tip by Day and Time')
plt.show()
```
这将生成以下图表:
![image.png](attachment:image.png)
用seaborn绘制堆积柱状图和折线图到一张画布上,堆积柱状图和折线图分别使用不同的Y轴,但是图例放在一起
以下是一个示例代码,其中使用seaborn中的titanic数据集来绘制堆积柱状图和折线图:
```python
import seaborn as sns
import matplotlib.pyplot as plt
# 加载数据集
titanic = sns.load_dataset('titanic')
# 创建画布和两个子图
fig, ax1 = plt.subplots(figsize=(10, 6))
ax2 = ax1.twinx()
# 绘制堆积柱状图
sns.countplot(x='class', hue='survived', data=titanic, ax=ax1)
ax1.set_ylabel('Count')
# 绘制折线图
sns.lineplot(x='class', y='fare', data=titanic, ax=ax2, color='r')
ax2.set_ylabel('Fare')
# 设置图例
ax1.legend(['Not Survived', 'Survived', 'Fare'], loc='upper left')
# 显示图形
plt.show()
```
结果如下图所示:
![stacked_bar_line_plot.png](https://i.loli.net/2021/06/02/RVpJbFw8sHdIeT2.png)
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)