Error Bars Matplotlib
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting http://stackoverflow.com/questions/22364565/python-pylab-scatter-plot-error-bars-the-error-on-each-point-is-unique ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a https://www.getdatajoy.com/examples/python-plots/bar-chart-with-error-bars minute: Sign up Python Pylab scatter plot error bars (the error on each point is unique) up vote 4 down vote favorite I am attempting a scatter plot of 2 arrays for which I have a third array containing the absolute error bars error (error in y direction) on each point. I want the error bars to between (point a - error on a) and (point a + error on a). Is there a way of achieving this with pylab and if not any ideas on how else I could do it? Thanks in advance python matplotlib share|improve this question asked Mar 12 '14 at 21:46 user3412782 31116 add a comment| 1 Answer 1 active oldest votes up vote 5 down vote accepted >>> import error bars matplotlib matplotlib.pyplot as plt >>> a = [1,3,5,7] >>> b = [11,-2,4,19] >>> plt.pyplot.scatter(a,b) >>> plt.scatter(a,b)
to plot multiple data sets in one chart, label the axes, show a legend, and display error bars. The code is based on the Bar Chart example, from the Matplotlib Examples. First, the required modules are imported. The array-manipulation module numpy and the matplotlib submodule pyplot, to plot 2d graphics. The corresponding aliases np and plt for these two modules are widely used conventions import numpy as np import matplotlib.pyplot as pltThe data to plot are 5 means for two different groups and the corresponding standard deviations, the first will determine the height of the bars and the latter the height of the error lines. For the colours it is possible to use html hexadecimal notation or html colour names. menMeans = (20, 35, 30, 35, 27) menStd = (2, 3, 4, 1, 2) womenMeans = (25, 32, 34, 20, 25) womenStd = (3, 5, 2, 3, 3) N = len(menMeans) # number of data entries ind = np.arange(N) # the x locations for the groups width = 0.35 # bar width fig, ax = plt.subplots() rects1 = ax.bar(ind, menMeans, # data width, # bar width color='MediumSlateBlue', # bar colour yerr=womenStd, # data for error bars error_kw={'ecolor':'Tomato', # error-bars colour 'linewidth':2}) # error-bar width rects2 = ax.bar(ind + width, womenMeans, width, color='Tomato', yerr=womenStd, error_kw={'ecolor':'MediumSlateBlue', 'linewidth':2}) axes = plt.gca() axes.set_ylim([0, 41]) # y-axis boundsThe next block of code adds some text for labels, title and axes ticks ax.set_ylabel('Scores') ax.set_title('Scores by group and gender') ax.set_xticks(ind + width) ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5')) ax.legend((rects1[0], rects2[0]), ('Men', 'Women'))This function prints the data on top of each bar with text(), it takes as arguments the x, y coordinates, the text itself and two alignment parameters. def autolabel(rects): for rect in rects: height = rect.get_height() ax.text(rect.get_x() + rect.get_width()/2., 1.05*height, '%d' % int(height), ha='center', # vertical alignment va='bottom' # horizontal alignment ) autolabel(rects1) autolabel(rects2) plt.show() # render the plotOutputCommentPlease enable JavaScript to view thecomments powered by Disqus.comments powered byDisqus© 2016 DataJoyTermsPrivacySecurityExamplesHelp GuidesAboutBlog