数据文档来源:
Table1的绘制
import pandas as pd
from tableone import TableOne
import matplotlib.pyplot as plt
# 获取数据
df = pd.read_csv('./sample.csv')
# 生成TableOne
columns_selected = ['age','sex','income','edu','paco21','chrpulhx','hrt1', 'neuro','sod1','death']
cateorder = {'income':['Under $11k','$11-$25k','$25-$50k','> $50k']}
table = TableOne(df, columns=columns_selected,
nonnormal=['edu'],
order=cateorder,
min_max=['age'],
groupby='death',
pval=True,
rename={'sex':'gender','death':'Mortality'})
print(table.tabulate(tablefmt='fancy_grid'))
绘制图
import pandas as pd
from tableone import TableOne
import matplotlib.pyplot as plt
# 获取数据
df = pd.read_csv('./sample.csv')
# 绘图
plt.figure(figsize=[10,10])
ax1 = plt.subplot2grid((4,6), (0,0), colspan=2, rowspan=2)
ax2 = plt.subplot2grid((4,6), (0,2), colspan=2, rowspan=2)
ax3 = plt.subplot2grid((4,6), (2,0), colspan=4, rowspan=2)
ax4 = plt.subplot2grid((4,6), (0,4), colspan=2, rowspan=4)
# 对于年龄绘制饼图
age_data = df['age']
# 将年龄划分成[18,30,50,70,90]
bins = [0, 30, 50, 70, 90, 120]
age_labels = ['<30','30-50','50-70','70-90','>90']
age_colors = ['wheat','lightgrey','rosybrown','darksalmon','skyblue']
cats = pd.cut(age_data, bins)
count = pd.value_counts(cats)
ax1.pie(count,
labels=age_labels,
colors=age_colors,
autopct='%1.2f%%',
pctdistance=0.7,
wedgeprops=dict(width=0.5))
ax1.set_title('Figure 1: Age Distribution')
# 绘制折线图
income = df['income']
edu = df['edu']
income_labels = ['<$11K', '$11-$25k', '$25-$50k', '>50K']
edu_11 = df.edu[df['income']=='Under $11k']
edu_25 = df.edu[df['income']=='$11-$25k']
edu_50 = df.edu[df['income']=='$25-$50k']
edu_above = df.edu[df['income']=='> $50k']
ax2.boxplot([edu_11, edu_25, edu_50, edu_above],
labels=income_labels,
showfliers=False)
ax2.set_title('Income and Education')
ax2.set_ylabel('Years of Education')
ax2.set_xlabel('Income')
# 绘制折线图
scatter_x = df['dschdte']
scatter_y = df['sadmdte']
ax3.scatter(scatter_x, scatter_y, s=5, c='c')
ax3.set_title('Relation of dschdte and sadmdte')
ax3.set_ylabel('sadmdte')
ax3.set_xlabel('dschdte')
# 绘制柱状图
white = len(df[df['race']=='white'])
black = len(df[df['race']=='black'])
other = len(df[df['race']=='other'])
bar_color = ['lightblue', 'wheat', 'lightgrey']
ax4.bar([0,1,2], [white, black, other], color=bar_color)
ax4.set_title('Race')
ax4.set_ylabel('# of patients')
ax4.set_xlabel('Race')
ax4.set_xticks([0,1,2])
ax4.set_xticklabels(['white','black','other'])
plt.suptitle('Data Report')
plt.tight_layout(rect=[0, 0.05, 1, 0.95])
plt.show()
原创文章(本站视频密码:66668888),作者:xujunzju,如若转载,请注明出处:https://zyicu.cn/?p=12000