简体   繁体   English

如何使用 python 制作带有误差线的 plot 条形图?

[英]How do I plot bar graphs with error bars using python?

I am using Python 3.5.我正在使用 Python 3.5。 Also, I am a beginner (3 weeks experience) Python attempter and somehow I haven't given up in trying to analyze my data.此外,我是初学者(3 周经验)Python 尝试者,不知何故我没有放弃尝试分析我的数据。

Data Description : My data is in a csv file (fev.csv).数据说明:我的数据在一个csv文件(fev.csv)中。 I've included it here if you want to see the full extent of it full data set .如果您想查看完整数据集的完整范围,我已将其包含在此处。 It has 5 columns:它有 5 列:

  • age (years)年龄(岁)
  • fev (liters) fev(升)
  • ht (inches)高(英寸)
  • sex (female=0, male=1)性别(女=0,男=1)
  • smoke (non-smoker=1, smoker=1)吸烟(非吸烟者=1,吸烟者=1)

Task : I am trying to write a program to generate a bar graph of average FEVs with error bars indicating standard deviation.任务:我正在尝试编写一个程序来生成平均 FEV 的条形图,其中误差条表示标准偏差。 I'm trying to get 2 side by side bars (smokers/non-smokers) at 4 different age categories (11-12, 13-14, 15-16, 17 or older).我试图在 4 个不同的年龄类别(11-12、13-14、15-16、17 或更大)获得 2 个并排的酒吧(吸烟者/非吸烟者)。

Code so far (please excuse all my #notes, it helps me know what I'm trying to do):到目前为止的代码(请原谅我所有的#notes,它帮助我知道我想做什么):

import numpy as np

import matplotlib.pyplot as plt

import pandas as pd



data = pd.read_csv('fev.csv')



nonsmokers = data[data.smoke==0]

smokers = data[data.smoke==1]



nonsmokers1 = nonsmokers[(nonsmokers.age==11) | (nonsmokers.age==12)]

nonsmokers2 = nonsmokers[(nonsmokers.age==13) | (nonsmokers.age==14)]

nonsmokers3 = nonsmokers[(nonsmokers.age==15) | (nonsmokers.age==16)]

nonsmokers4 = nonsmokers[(nonsmokers.age>=17)]



smokers1 = smokers[(smokers.age==11) | (smokers.age==12)]

smokers2 = smokers[(smokers.age==13) | (smokers.age==14)]

smokers3 = smokers[(smokers.age==15) | (smokers.age==16)]

smokers4 = smokers[(smokers.age>=17)]



nonsmMean = [nonsmokers1.fev.mean(), nonsmokers2.fev.mean(), nonsmokers3.fev.mean(), nonsmokers4.fev.mean()]

nonsmSd = [nonsmokers1.fev.std(), nonsmokers2.fev.std(), nonsmokers3.fev.std(), nonsmokers4.fev.std()]

smMean = [smokers1.fev.mean(), smokers2.fev.mean(), smokers3.fev.mean(), smokers4.fev.mean()]

smSd = [smokers1.fev.std(), smokers2.fev.std(), smokers3.fev.std(), smokers4.fev.std()]



# data to be plotted

nonsmoker = np.array(nonsmMean)

sdNonsmoker = np.array(nonsmSd)

smoker = np.array(smMean)

sdSmoker = np.array(smSd)



# parameters

bar_width = 0.35

x = np.arange(len(nonsmoker))



# plotting bars

plt.bar(x, nonsmoker, bar_width, yerr=sdNonsmoker, ecolor='k', color='b', label='Nonsmokers')

plt.bar(x+bar_width, smoker, bar_width, yerr=sdSmoker, ecolor='k', color='m', label='Smokers')



# formatting and labeling the axes and title

plt.xlabel('Age')

plt.ylabel('FEV')

plt.title('Mean FEV by Age and Smoking Status')



plt.xticks(x+0.35, ['11 to 12', '13 to 14', '15 to 16', '17+'])



# adding the legend

plt.legend()



plt.axis([-0.5,4.2,0,7])



plt.savefig('FEVgraph.png', dpi=300)


# and we are done!

plt.show() 

Is there a more efficient way of doing this?有没有更有效的方法来做到这一点?

Thanks!谢谢!

Possible solution is the following:可能的解决方案如下:

# pip install pandas
# pip install matplotlib

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# read csv file and create pandas dataframe
df = pd.read_csv('https://raw.githubusercontent.com/benkeser/halplus/master/inst/extdata/fev.csv')

# assign age bins to data
bins = [df['age'].min()-1, 10, 12, 14, 16, df['age'].max()]
bins_labels = ['<11', '11 to 12', '13 to 14', '15 to 16', '17+']
df['age_bins'] = pd.cut(df['age'], bins, labels = bins_labels)

# aggregate data
result = df.groupby(['smoke', 'age_bins'], as_index=False).agg({'fev':['mean','std']})
result.columns = ['_'.join(col).strip('_') for col in result.columns.values]
result = result.round(1)

# prepare data for plot
nonsmokers = result[result['smoke'] == 0]
smokers = result[result['smoke'] == 1]
x = np.arange(len(bins_labels))
width = 0.35

# set plot fugure size
plt.rcParams["figure.figsize"] = [8,6]


fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, nonsmokers['fev_mean'], width, yerr=nonsmokers['fev_std'], color='b', label='Nonsmokers')
rects2 = ax.bar(x + width/2, smokers['fev_mean'], width, yerr=smokers['fev_std'], color='m', label='Smokers')

ax.set_xlabel('Age')
ax.set_ylabel('FEV')
ax.set_title('Mean FEV by Age and Smoking Status')
ax.set_xticks(x, bins_labels)
ax.legend(loc=2)

fig.tight_layout()

plt.savefig('FEVgraph.png', dpi=300)

plt.show()

Returns退货

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python的条形图中每个轴绘制多组条形? - How do I plot more than one set of bars per axis on a bar plot in python? 我在matplotlib(python)中有一个带有误差条的条形图,但我希望误差条位于该条的中间。 我该怎么做呢? 谢谢 - I have a bar chart with error bars in matplotlib (python), but i want the error bars in the middle of the bar. how do i do this? Thanks 如何在Python的数据框列中绘制唯一条目的条形图? - How do I plot bar graphs for unique entries in a dataframe column in Python? 如何在 Python 中使用 pandas 和 matplotlib 绘制条形图时删除条形之间的空间? - How can I remove space between bars while plotting bar plot using pandas and matplotlib in Python? 如何将错误栏添加到分组栏 plot? - How to add error bars to a grouped bar plot? 如何绘制一维误差条形图? - Python - How do I plot a 1 dimensional error bar graph? - python 如何使用 Python(或 R)在条形图中进一步分组的条形图中的 plot 堆叠条形图 - How to plot stacked bars within grouped bars within further grouped bars in a bar-chart using Python (or R) 如何使用python pandas将误差线添加到分组的条形图中? - how do I add error bars to a grouped bar chart with python pandas? 如何裁剪熊猫图中的误差线? - How do I clip error bars in a pandas plot? 如何使用matplotlib绘制多个图形? - How do I plot multiple graphs using matplotlib?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM