sns.displot(values, bins=100, color='k'),怎么添加概率密度曲线
时间: 2024-02-02 10:04:52 浏览: 26
要在Seaborn的displot中添加概率密度曲线,可以使用kde参数。将kde参数设置为True即可添加概率密度曲线。例如:
```
sns.displot(values, bins=100, color='k', kde=True)
```
这将绘制一张直方图和一条概率密度曲线,其中values是数据的数组或Series对象。您可以根据需要调整其他参数,例如颜色,带宽等。
相关问题
x = np.random.randn(100) ax = sns.displot(x) x = np.random.randn(100) ax = sns.displot(x) x = np.random.randn(100) ax = sns.displot(x) x = np.random.randn(100) ax = sns.displot(x) x = np.random.randn(100)ax = sns.displot(x) 写到pycharm里,pycharm为什么不出现图
在PyCharm中,你需要使用`plt.show()`来显示图形。添加这一行代码后,你的图形应该能够在PyCharm中正确显示。请尝试将以下代码添加到你的脚本中:
```python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_theme()
np.random.seed(0)
x = np.random.randn(100)
ax = sns.displot(x)
plt.show()
x = np.random.randn(100)
ax = sns.displot(x)
plt.show()
x = np.random.randn(100)
ax = sns.displot(x)
plt.show()
x = np.random.randn(100)
ax = sns.displot(x)
plt.show()
x = np.random.randn(100)
ax = sns.displot(x)
plt.show()
```
这样你就可以在PyCharm中看到生成的图形了。如果还有其他问题,请随时提问!
fig, axs = plt.subplots(1, 4, figsize=(15, 4)) sns.displot(adata.obs["total_counts"], kde=False, ax=axs[0]) sns.displot(adata.obs["total_counts"][adata.obs["total_counts"] < 10000], kde=False, bins=40, ax=axs[1]) sns.displot(adata.obs["n_genes_by_counts"], kde=False, bins=60, ax=axs[2]) sns.displot(adata.obs["n_genes_by_counts"][adata.obs["n_genes_by_counts"] < 4000], kde=False, bins=60, ax=axs[3]) plt.show()完善这段代码使图像在一个画布上生成
可以使用`plt.subplots()`函数来创建一个包含所有子图的画布。修改代码如下:
```python
fig, axs = plt.subplots(1, 4, figsize=(15, 4))
sns.displot(adata.obs["total_counts"], kde=False, ax=axs[0])
sns.displot(adata.obs["total_counts"][adata.obs["total_counts"] < 10000], kde=False, bins=40, ax=axs[1])
sns.displot(adata.obs["n_genes_by_counts"], kde=False, bins=60, ax=axs[2])
sns.displot(adata.obs["n_genes_by_counts"][adata.obs["n_genes_by_counts"] < 4000], kde=False, bins=60, ax=axs[3])
# 移除原始的plt.show()
plt.tight_layout() # 调整子图之间的间距
plt.show()
```
这样修改后,所有的子图将在一个画布上生成,并且通过`plt.tight_layout()`函数可以调整子图之间的间距,以确保它们不会重叠。