sns.countplot(x='diabetes', data=df) plt.title('Diabetes Distribution') plt.show()照上
时间: 2023-12-10 17:05:22 浏览: 134
要在条形图上显示每个条形的具体数值,可以添加以下代码:
```python
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv('data.csv')
sns.countplot(x='diabetes', data=df)
# 添加具体数值
for i in range(len(df['diabetes'].value_counts().index)):
plt.text(i,
df['diabetes'].value_counts()[i],
df['diabetes'].value_counts()[i],
ha='center',
va='bottom')
plt.title('Diabetes Distribution')
plt.show()
```
这样就会在条形图上显示每个条形的具体数值。
相关问题
for col in ['hypertension', 'heart_disease', 'diabetes']: sns.countplot(x=col, data=df) plt.title(f'{col} Distribution') plt.show()照上
要在三个条形图上都显示每个条形的具体数值,可以添加以下代码:
```python
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv('data.csv')
for col in ['hypertension', 'heart_disease', 'diabetes']:
sns.countplot(x=col, data=df)
# 添加具体数值
for i in range(len(df[col].value_counts().index)):
plt.text(i,
df[col].value_counts()[i],
df[col].value_counts()[i],
ha='center',
va='bottom')
plt.title(f'{col} Distribution')
plt.show()
```
这样就会在三个条形图上都显示每个条形的具体数值。
sns.countplot(x='gender', hue='diabetes', data=df) plt.title('Gender vs Diabetes') plt.show()照上
要在带有分组的条形图上显示每个条形的具体数值,可以添加以下代码:
```python
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv('data.csv')
sns.countplot(x='gender', hue='diabetes', data=df)
# 添加具体数值
for i in range(len(df['gender'].value_counts().index)):
for j in range(len(df['diabetes'].value_counts().index)):
plt.text(j + i*2 - 0.2,
df[(df['gender'] == df['gender'].value_counts().index[i]) & (df['diabetes'] == df['diabetes'].value_counts().index[j])]['gender'].count(),
df[(df['gender'] == df['gender'].value_counts().index[i]) & (df['diabetes'] == df['diabetes'].value_counts().index[j])]['gender'].count())
plt.title('Gender vs Diabetes')
plt.show()
```
这样就会在带有分组的条形图上显示每个条形的具体数值。
阅读全文