Python 在条形图上打印图形并包含缺少的值编号
python新手,我有基于医院的医疗数据。现在,我确实希望绘制每家医院的数据、收集时间和患者数量,但我也希望在图表中包括每家医院缺失数据的数量,以便我还可以知道每家医院未输入的月份数据。这是我的数据的一个片段Python 在条形图上打印图形并包含缺少的值编号,python,matplotlib,plotly,Python,Matplotlib,Plotly,python新手,我有基于医院的医疗数据。现在,我确实希望绘制每家医院的数据、收集时间和患者数量,但我也希望在图表中包括每家医院缺失数据的数量,以便我还可以知道每家医院未输入的月份数据。这是我的数据的一个片段 data = {'Hosp_name':['Hos1', 'Hos1', 'Hos2', 'Hos2','Hos3','Hos3'], 'Period':['20-Apr', '21-Apr', '20-Apr', '21-Aug','20-Apr','21-Apr'],
data = {'Hosp_name':['Hos1', 'Hos1', 'Hos2', 'Hos2','Hos3','Hos3'], 'Period':['20-Apr', '21-Apr', '20-Apr', '21-Aug','20-Apr','21-Apr'],
'Num_of_patients':[30,'NAN',45,56,'NAN',67]}
df = pd.DataFrame(data)
如何转换这些数据,以便正确绘制并在图形中包含缺失的数据。这就是我试过的
import plotly.express as px
fig = px.bar(df, x='Hosp_name', y=['period','Num_of_patients'])
fig.show()
预期输出是每个医院的条形图,显示收集的期间数据、缺失值的数量和患者数量这是我的版本,我是如何理解它的,以及matplotlib将如何使用它的
import pandas as pd
data = {'Hosp_name':['Hos1', 'Hos1', 'Hos2', 'Hos2','Hos3','Hos3'], 'Period':['20-Apr', '21-Apr', '20-Apr', '21-Aug','20-Apr','21-Apr'], 'Num_of_patients':[30,'NAN',45,56,'NAN',67]}
df = pd.DataFrame(data).groupby(['Period','Hosp_name'])[['Num_of_patients']].sum()#.reset_index().set_index('Period')
df['Num_of_patients'] = pd.to_numeric(df['Num_of_patients'], errors = 'coerce')
Out[1]:
Num_of_patients
Period Hosp_name
20-Apr Hos1 30.0
Hos2 45.0
Hos3 NaN
21-Apr Hos1 NaN
Hos3 67.0
21-Aug Hos2 56.0
df1 = df.pivot_table(index = 'Period', values = 'Num_of_patients', columns = 'Hosp_name').fillna(0)
df1
Out[2]:
Hosp_name Hos1 Hos2 Hos3
Period
20-Apr 30.0 45.0 0.0
21-Apr 0.0 0.0 67.0
21-Aug 0.0 56.0 0.0
df1 = df1.reset_index()
import matplotlib.pyplot as plt
%matplotlib inline
for i,col in enumerate(df1.columns[1:]):
bars= plt.bar([x+i*0.3 for x in list(df1.index)], df1[col], width = 0.3, label = col)
for bar in bars:
plt.gca().text(bar.get_x() + bar.get_width()/2, bar.get_height() - 5, str(int(bar.get_height())),ha='center', color='w', fontsize=11)
plt.xticks(list(df1.index), df1['Period'])
plt.legend()
对于一组列
下面是可视化的完整代码(度量的不同图形)
你可以做任何你想做的事
import matplotlib.pyplot as plt
%matplotlib inline
level_1 = ['Num_of_patients', 'age', 'patient_visits']
level_2 = ['Hos1', 'Hos2', 'Hos3']
color = ['r','g','b']
fig, ax = plt.subplots(3,1, sharex = True, gridspec_kw={'hspace': 0.3}, figsize=(10,5))
for i, metric in enumerate(level_1):
for j, hosp in enumerate(level_2):
bars = ax[i].bar([x+j*0.2 for x in list(df1.index)], df1[(metric, hosp)], width = 0.2, color = color[j], label = hosp)
for bar in bars:
ax[i].text(bar.get_x() + bar.get_width()/2, bar.get_height() - 8, str(int(bar.get_height())),
ha='center', color='w', fontsize=8)
ax[i].set_title(f'{metric}', loc = 'right')
ax[i].legend()
plt.xticks(list(df1.index), df1['Period'])
Hosps的不同图
请提供一个最小的可运行示例。
c
来自哪里?修正你的报价。预期产量是多少?每对(医院、周期)一列?您想如何显示缺失值?@AlexisBRENON该代码是错误的,尽管我的预期输出是每个医院都有自己的条形图,每个医院都有缺失值的计数并显示在图形上。这将有点棘手,因为患者数量和缺失值的数量不在同一个“单位”中。想象一下,一家医院一个周期有100万病人,第二个周期有一个NAN病人。在同一绘图上绘制两个值将使缺失值的计数太小。。。您可能应该使用,或者可能在两个不同的轴上绘制它们。是否可以在代码中使用更多的列,我有更多的列包含在num of patients列中。如果可能的话,我可以看到如何添加它们,它已经能够处理其他列。可能是循环中唯一的修改,指向exactli columns name(df1.columns[1:]这里就是,例如,输入['Hos1','Hos2'))并更改宽度的系数(不是0.3,而是1/列数),我在这个级别添加列['Num_of_of_patients']),但如果我打印它告诉我找不到所有其他列,它只画了一列,你是说这样的结构?data={'Hosp_name':[…],'Period':[…],'Num_of_patients':[…],'ONE_MORE_COLUMN':[…])我的意思是像这个pd.DataFrame(data.groupby(['Period','Hosp_name'))[['Num of_of_patients','age','patient_visions'].sum()
import matplotlib.pyplot as plt
%matplotlib inline
level_1 = ['Num_of_patients', 'age', 'patient_visits']
level_2 = ['Hos1', 'Hos2', 'Hos3']
color = ['r','g','b']
fig, ax = plt.subplots(3,1, sharex = True, gridspec_kw={'hspace': 0.3}, figsize=(10,5))
for i, metric in enumerate(level_1):
for j, hosp in enumerate(level_2):
bars = ax[i].bar([x+j*0.2 for x in list(df1.index)], df1[(metric, hosp)], width = 0.2, color = color[j], label = hosp)
for bar in bars:
ax[i].text(bar.get_x() + bar.get_width()/2, bar.get_height() - 8, str(int(bar.get_height())),
ha='center', color='w', fontsize=8)
ax[i].set_title(f'{metric}', loc = 'right')
ax[i].legend()
plt.xticks(list(df1.index), df1['Period'])
import matplotlib.pyplot as plt
%matplotlib inline
level_1 = ['Hos1', 'Hos2', 'Hos3']
level_2 = ['Num_of_patients', 'age', 'patient_visits']
color = ['r','g','b']
fig, ax = plt.subplots(3,1, sharex = True, gridspec_kw={'hspace': 0.3}, figsize=(5,5))
for i, hosp in enumerate(level_1):
for j, metric in enumerate(level_2):
bars = ax[i].bar([x+j*0.2 for x in list(df1.index)], df1[(metric, hosp)], width = 0.2, color = color[j], label = metric)
for bar in bars:
ax[i].text(bar.get_x() + bar.get_width()/2, bar.get_height() - 8, str(int(bar.get_height())),
ha='center', color='w', fontsize=8)
ax[i].set_title(f'{metric}', loc = 'right')
ax[i].legend()
_ = plt.xticks(list(df1.index), df1['Period'])