How to plot min and max levels from an averaged dataframe [duplicate] - python

I'd like to adapt my plotting code in order to show min/max bar as depicted in the figure below:
My code is:
from datetime import datetime, timedelta
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style("white")
sns.set_style('darkgrid',{"axes.facecolor": ".92"}) # (1)
sns.set_context('notebook')
Delay = ['S1', 'S2', 'S3', 'S4']
Time = [87, 66, 90, 55]
df = pd.DataFrame({'Delay':Delay,'Time':Time})
print("Accuracy")
display(df) # in jupyter
fig, ax = plt.subplots(figsize = (8,6))
x = Delay
y = Time
plt.xlabel("Delay", size=14)
plt.ylim(-0.3, 100)
width = 0.1
for i, j in zip(x,y):
ax.bar(i,j, edgecolor = "black",
error_kw=dict(lw=1, capsize=1, capthick=1))
ax.set(ylabel = 'Accuracy')
from matplotlib import ticker
ax.yaxis.set_major_locator(ticker.MultipleLocator(10))
plt.savefig("Try.png", dpi=300, bbox_inches='tight')
The code produce this figure:
The min/max I want to add is for:
87 (60-90)
66 (40-70)
90 (80-93)
55 (23-60)
Thanks in advance for help.

This answer expands on the code from your previous question, by including examples for seaborn.barplot and ax.bar.
Also see Different ways of specifying error bars & matplotlib.pyplot.errorbar
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# set edgecolor param (this is a global setting, so only set it once)
plt.rcParams["patch.force_edgecolor"] = True
# setup the dataframe
Delay = ['S1', 'S2', 'S3', 'S4']
Time = [87, 66, 90, 55]
df = pd.DataFrame({'Delay':Delay,'Time':Time})
# create a dict for the errors
error = {87: {'max': 90,'min': 60}, 66: {'max': 70,'min': 40}, 90: {'max': 93,'min': 80}, 55: {'max': 60,'min': 23}}
seaborn.barplot
seaborn.barplot will add error bars automatically, as shown in the examples at the link. However, this is specific to using many data points. In this case, a value is being specified as the error, the error is not being determined from the data.
When error bars are added in this way, the capsize parameter can be specified, to add horizontal lines at the top and bottom of the error bar.
# plot the figure
fig, ax = plt.subplots(figsize=(8, 6))
sns.barplot(x='Delay', y='Time', data=df, ax=ax)
# add the lines for the errors
for p in ax.patches:
x = p.get_x() # get the bottom left x corner of the bar
w = p.get_width() # get width of bar
h = p.get_height() # get height of bar
min_y = error[h]['min'] # use h to get min from dict z
max_y = error[h]['max'] # use h to get max from dict z
plt.vlines(x+w/2, min_y, max_y, color='k') # draw a vertical line
As noted in the answer from gepcel, the yerr parameter can be used to explicitly provide errors to the API.
However, the format of your errors is not correct for the parameter. yerr expects the values to be in relation to the top of the bar
S1 is 87, with min or 60 and max of 90. Therefore, ymin is 27, (87-60), and ymax is 3, (90-87).
The seaborn.barplot capsize parameter doesn't seem to work with yerr, so you must set the matplotlib 'errorbar.capsize' rcParmas. See Matplotlib Errorbar Caps Missing
# set capsize param (this is a global setting, so only set it once)
plt.rcParams['errorbar.capsize'] = 10
# create dataframe as shown by gepcel
Delay = ['S1', 'S2', 'S3', 'S4']
Time = [87, 66, 90, 55]
_min = [60, 40, 80, 23]
_max = [90, 70, 93, 60]
df = pd.DataFrame({'Delay':Delay,'Time':Time, 'Min': _min, 'Max': _max})
# create ymin and ymax
df['ymin'] = df.Time - df.Min
df['ymax'] = df.Max - df.Time
# extract ymin and ymax into a (2, N) array as required by the yerr parameter
yerr = df[['ymin', 'ymax']].T.to_numpy()
# plot with error bars
fig, ax = plt.subplots(figsize=(8, 6))
sns.barplot(x='Delay', y='Time', data=df, yerr=yerr, ax=ax)
pandas.DataFrame.plot.bar
fig, ax = plt.subplots(figsize=(8, 6))
df.plot.bar(x='Delay', ax=ax)
for p in ax.patches:
x = p.get_x() # get the bottom left x corner of the bar
w = p.get_width() # get width of bar
h = p.get_height() # get height of bar
min_y = error[h]['min'] # use h to get min from dict z
max_y = error[h]['max'] # use h to get max from dict z
plt.vlines(x+w/2, min_y, max_y, color='k') # draw a vertical line
ax.bar
fig, ax = plt.subplots(figsize=(8, 6))
ax.bar(x='Delay', height='Time', data=df)
for p in ax.patches:
x = p.get_x() # get the bottom left x corner of the bar
w = p.get_width() # get width of bar
h = p.get_height() # get height of bar
min_y = error[h]['min'] # use h to get min from dict z
max_y = error[h]['max'] # use h to get max from dict z
plt.vlines(x+w/2, min_y, max_y, color='k') # draw a vertical line

You can use yerr arg of plt.bar directly. Using #Trenton McKinney's code for an example:
import pandas as pd
import matplotlib.pyplot as plt
# setup the dataframe
Delay = ['S1', 'S2', 'S3', 'S4']
Time = [87, 66, 90, 55]
_min = [60, 40, 80, 23]
_max = [90, 70, 93, 60]
df = pd.DataFrame({'Delay':Delay,'Time':Time, 'Min': _min, 'Max': _max})
df = (df.assign(yerr_min = df.Time-df.Min)
.assign(yerr_max=df.Max-df.Time))
plt.figure(figsize=(8, 6))
plt.bar(x='Delay', height='Time', yerr=df[['yerr_min', 'yerr_max']].T.values, capsize=10, data=df)
plt.show()

Here's a solution using yerr and numpy. It has less boilerplate code than #gepcel's.
import matplotlib.pyplot as plt
import numpy as np
Delay = ['S1', 'S2', 'S3', 'S4'] # Categories
Time = [87, 66, 90, 55]
_min = [60, 40, 80, 23]
_max = [90, 70, 93, 60]
plt.figure(figsize=(8, 6))
yerr = [np.subtract(Time, _min), np.subtract(_max, Time)]
plt.bar(Delay, Time, yerr=yerr, capsize=10)
plt.show()

Related

How to plot min/max bars with a bar plot

I'd like to adapt my plotting code in order to show min/max bar as depicted in the figure below:
My code is:
from datetime import datetime, timedelta
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style("white")
sns.set_style('darkgrid',{"axes.facecolor": ".92"}) # (1)
sns.set_context('notebook')
Delay = ['S1', 'S2', 'S3', 'S4']
Time = [87, 66, 90, 55]
df = pd.DataFrame({'Delay':Delay,'Time':Time})
print("Accuracy")
display(df) # in jupyter
fig, ax = plt.subplots(figsize = (8,6))
x = Delay
y = Time
plt.xlabel("Delay", size=14)
plt.ylim(-0.3, 100)
width = 0.1
for i, j in zip(x,y):
ax.bar(i,j, edgecolor = "black",
error_kw=dict(lw=1, capsize=1, capthick=1))
ax.set(ylabel = 'Accuracy')
from matplotlib import ticker
ax.yaxis.set_major_locator(ticker.MultipleLocator(10))
plt.savefig("Try.png", dpi=300, bbox_inches='tight')
The code produce this figure:
The min/max I want to add is for:
87 (60-90)
66 (40-70)
90 (80-93)
55 (23-60)
Thanks in advance for help.
This answer expands on the code from your previous question, by including examples for seaborn.barplot and ax.bar.
Also see Different ways of specifying error bars & matplotlib.pyplot.errorbar
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# set edgecolor param (this is a global setting, so only set it once)
plt.rcParams["patch.force_edgecolor"] = True
# setup the dataframe
Delay = ['S1', 'S2', 'S3', 'S4']
Time = [87, 66, 90, 55]
df = pd.DataFrame({'Delay':Delay,'Time':Time})
# create a dict for the errors
error = {87: {'max': 90,'min': 60}, 66: {'max': 70,'min': 40}, 90: {'max': 93,'min': 80}, 55: {'max': 60,'min': 23}}
seaborn.barplot
seaborn.barplot will add error bars automatically, as shown in the examples at the link. However, this is specific to using many data points. In this case, a value is being specified as the error, the error is not being determined from the data.
When error bars are added in this way, the capsize parameter can be specified, to add horizontal lines at the top and bottom of the error bar.
# plot the figure
fig, ax = plt.subplots(figsize=(8, 6))
sns.barplot(x='Delay', y='Time', data=df, ax=ax)
# add the lines for the errors
for p in ax.patches:
x = p.get_x() # get the bottom left x corner of the bar
w = p.get_width() # get width of bar
h = p.get_height() # get height of bar
min_y = error[h]['min'] # use h to get min from dict z
max_y = error[h]['max'] # use h to get max from dict z
plt.vlines(x+w/2, min_y, max_y, color='k') # draw a vertical line
As noted in the answer from gepcel, the yerr parameter can be used to explicitly provide errors to the API.
However, the format of your errors is not correct for the parameter. yerr expects the values to be in relation to the top of the bar
S1 is 87, with min or 60 and max of 90. Therefore, ymin is 27, (87-60), and ymax is 3, (90-87).
The seaborn.barplot capsize parameter doesn't seem to work with yerr, so you must set the matplotlib 'errorbar.capsize' rcParmas. See Matplotlib Errorbar Caps Missing
# set capsize param (this is a global setting, so only set it once)
plt.rcParams['errorbar.capsize'] = 10
# create dataframe as shown by gepcel
Delay = ['S1', 'S2', 'S3', 'S4']
Time = [87, 66, 90, 55]
_min = [60, 40, 80, 23]
_max = [90, 70, 93, 60]
df = pd.DataFrame({'Delay':Delay,'Time':Time, 'Min': _min, 'Max': _max})
# create ymin and ymax
df['ymin'] = df.Time - df.Min
df['ymax'] = df.Max - df.Time
# extract ymin and ymax into a (2, N) array as required by the yerr parameter
yerr = df[['ymin', 'ymax']].T.to_numpy()
# plot with error bars
fig, ax = plt.subplots(figsize=(8, 6))
sns.barplot(x='Delay', y='Time', data=df, yerr=yerr, ax=ax)
pandas.DataFrame.plot.bar
fig, ax = plt.subplots(figsize=(8, 6))
df.plot.bar(x='Delay', ax=ax)
for p in ax.patches:
x = p.get_x() # get the bottom left x corner of the bar
w = p.get_width() # get width of bar
h = p.get_height() # get height of bar
min_y = error[h]['min'] # use h to get min from dict z
max_y = error[h]['max'] # use h to get max from dict z
plt.vlines(x+w/2, min_y, max_y, color='k') # draw a vertical line
ax.bar
fig, ax = plt.subplots(figsize=(8, 6))
ax.bar(x='Delay', height='Time', data=df)
for p in ax.patches:
x = p.get_x() # get the bottom left x corner of the bar
w = p.get_width() # get width of bar
h = p.get_height() # get height of bar
min_y = error[h]['min'] # use h to get min from dict z
max_y = error[h]['max'] # use h to get max from dict z
plt.vlines(x+w/2, min_y, max_y, color='k') # draw a vertical line
You can use yerr arg of plt.bar directly. Using #Trenton McKinney's code for an example:
import pandas as pd
import matplotlib.pyplot as plt
# setup the dataframe
Delay = ['S1', 'S2', 'S3', 'S4']
Time = [87, 66, 90, 55]
_min = [60, 40, 80, 23]
_max = [90, 70, 93, 60]
df = pd.DataFrame({'Delay':Delay,'Time':Time, 'Min': _min, 'Max': _max})
df = (df.assign(yerr_min = df.Time-df.Min)
.assign(yerr_max=df.Max-df.Time))
plt.figure(figsize=(8, 6))
plt.bar(x='Delay', height='Time', yerr=df[['yerr_min', 'yerr_max']].T.values, capsize=10, data=df)
plt.show()
Here's a solution using yerr and numpy. It has less boilerplate code than #gepcel's.
import matplotlib.pyplot as plt
import numpy as np
Delay = ['S1', 'S2', 'S3', 'S4'] # Categories
Time = [87, 66, 90, 55]
_min = [60, 40, 80, 23]
_max = [90, 70, 93, 60]
plt.figure(figsize=(8, 6))
yerr = [np.subtract(Time, _min), np.subtract(_max, Time)]
plt.bar(Delay, Time, yerr=yerr, capsize=10)
plt.show()

Single stacked bar chart with custom gradient coloring

Here's what I came up with by plotting thick line segments.
The coloration is blue, with varying alpha, 0 < alpha < 1.
My workaround doens't work as I'd like because I don't have a legend (I want a legend that shows a gradient of the blue at varying alpha).
Additionally, I've found that matplotlib scales funny. There should be no overlap of the bars, but if I adjust the window size, the gap between the line segments will change.This is the same figure as the earlier one, just after I've resized the figure window with my mouse.
I'm not sure if there's a better way to go about accomplishing this, or if there's a different package I can use.
Here's the snippet of code that I'm using.
import matplotlib.pyplot as plt
x1 =[0, 19, 39, 46, 60, 79]
x2 = [19, 39, 46, 60, 79, 90]
alpha_list = [-0.8402, -0.6652, 0.0, -0.5106, -0.8074, 0.0]
plt.figure()
for idx,x in enumerate(x1):
plt.plot([x1[idx],x2[idx]],[0,0],color = 'blue',alpha=alpha_list[idx],linewidth =20)
plt.show()
I suppose alpha is just a workaround for using different shades of blue? In that case the Blues colormap can be used instead.
Several lines can be plotted using a LineCollection.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
x1 =[0, 19, 39, 46, 60, 79]
x2 = [19, 39, 46, 60, 79, 90]
alpha_list = [-0.8402, -0.6652, 0.0, -0.5106, -0.8074, 0.0]
verts = np.dstack((np.c_[x1, x2], np.zeros((len(x1), 2))))
fig, ax = plt.subplots()
lc = LineCollection(verts, linewidth=40, cmap="Blues_r", array=np.array(alpha_list))
ax.add_collection(lc)
ax.autoscale()
ax.set_ylim(-1,1)
fig.colorbar(lc)
plt.show()
I think a workaround would be to use plt.barh. Here is an example using normalized color maps. Each color gets converted to RGBA before it can be passed to plt.barh.
import matplotlib.pyplot as plt
from matplotlib import colors
import matplotlib.cm as cmx
x1 =[0, 19, 39, 46, 60, 79]
x2 = [19, 39, 46, 60, 79, 90]
values = range(len(x1))
jet = cm = plt.get_cmap('jet')
cNorm = colors.Normalize(vmin=0, vmax=values[-1])
scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=jet)
fig, ax = plt.subplots()
for idx, x, y in zip(values,x1, x2):
colorVal = scalarMap.to_rgba(values[idx])
start = x
end = y
width=end-start
ax.barh(y = 0, width = width, left=start, height = 0.1, label = str(idx), color=colorVal)
ax.set_ylim(-.5,0.5)
ax.legend()
which returns:
If you really want to just change the alpha transparency of a single color, you would just have to input alpha_list[idx] for the last element to the RGBA tuple colorVal. For some reason, RGBA did not like negative alpha values, so notice I changed them all to positive
fig, ax = plt.subplots()
alpha_list = [0.8402, 0.6652, 0.01, 0.5106, 0.8074, 0.0]
for idx, x, y in zip(values,x1, x2):
colorVal = (0.0, 0.3, 1.0, alpha_list[idx])
start = x
end = y
width=end-start
ax.barh(y = 0, width = width, left=start, height = 0.1, label = str(idx), color=colorVal)
ax.set_ylim(-.5,0.5)
ax.legend()

Plot a histogram using Python with manual bins

I am trying to plot a histogram using the matplotlib.hist() function.
Below code is not getting the correct histogram
X axis is the years (age), X axis I want to have 0 20, 25, 30, 35, 40, 45, 50, 55
Y axis is the probability
Someone can help me to get the correct histogram ?
import matplotlib.pyplot as plt
list_age = ['26','28','26','36','38','31','22','31','25','30','37','27','27','29','27','21','27','38','31','41','28','31','28','33','26','39','37','24','31','34','39','33','22', '30','24','29','28','34','27','28','26','26','25','40','24','37','24','28','26','29','26','31','23','31','36','32','25','31','25','33','36','27','28',
'25','27','39','36','30','31','34','23','31','32','31','33','32','39','35','35','22','34','25','35','35','41','20','21','35','32','30','22','21','23','33','25','30','24','39','24','27','22','33','30','27','30','23','29','30','22','31','29','31','24','29','25','24','26','29','31','24','32','21','25','29','30']
list_age.sort()
bins = 55
plt.hist(list_age, bins, facecolor='g')
plt.xlabel('Years')
plt.ylabel('Probability')
plt.grid(True)
plt.show()
You need to convert your list_age to a list of integers first (instead of a list of strings).
Then, simply use the options density (or normed) to display probability and xticks to change the ticks for the x-axis.
import matplotlib.pyplot as plt
list_age = ['26','28','26','36','38','31','22','31','25','30','37','27','27','29','27','21','27','38','31','41','28','31','28','33','26','39','37','24','31','34','39','33','22', '30','24','29','28','34','27','28','26','26','25','40','24','37','24','28','26','29','26','31','23','31','36','32','25','31','25','33','36','27','28',
'25','27','39','36','30','31','34','23','31','32','31','33','32','39','35','35','22','34','25','35','35','41','20','21','35','32','30','22','21','23','33','25','30','24','39','24','27','22','33','30','27','30','23','29','30','22','31','29','31','24','29','25','24','26','29','31','24','32','21','25','29','30']
list_age = [ int(i) for i in list_age ]
bins = len(set(list_age))
plt.hist(list_age, bins = bins, density = True, facecolor = "g") # Replace density by normed if older version of matplotlib
plt.xticks(range(0, 55, 5))
plt.xlabel('Years')
plt.ylabel('Probability')
plt.grid(True)
plt.show()
If you want to display the bars at specific bins, simply define bins at their coordinates:
plt.hist(list_age, bins = [ 0, 20, 25, 30, 35, 40, 45, 50, 55 ], density = True, facecolor = "g")

Python animated radar chart

I'm trying to create a radar chart using Python / Matplotlib where measured data can be "played back" using matplotlib's built in animation module. I want the data points to move along their respective axes as the data set is traversed. I have problems reading the data and updating the chart, nor am I able to find an example of this.
I have attached a piece of code that should give you an idea of what I am trying to achieve:
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from math import pi
class SubplotAnimation(animation.TimedAnimation):
def __init__(self, data):
self.data = data
fig = plt.figure()
ax = fig.add_subplot(111, projection='polar')
# Figure definition
cat = ['A', 'B', 'C', 'D', 'E']
values = [10, 10, 10, 10, 10]
N = len(cat)
x_as = [n / float(N) * 2 * pi for n in range(N)]
# Because our chart will be circular we need to append a copy of
# the first value of each list at the end of each list with data
values += values[:1]
x_as += x_as[:1]
plt.rc('axes', linewidth=0.5, edgecolor='#888888') # Set color of axes
# Create polar plot
ax = plt.subplot(111, projection='polar')
# Set clockwise rotation. That is:
ax.set_theta_offset(pi / 2)
ax.set_theta_direction(-1)
# Set position of y-labels
ax.set_rlabel_position(0)
# Set color and linestyle of grid
ax.xaxis.grid(True, color="#888888", linestyle='solid', linewidth=0.5)
ax.yaxis.grid(True, color="#888888", linestyle='solid', linewidth=0.5)
# Set number of radial axes and remove labels
plt.xticks(x_as[:-1], [])
# Set yticks
plt.yticks([20, 40, 60, 80, 100], ["20", "40", "60", "80", "100"])
# Set axes limits
plt.ylim(0, 100)
# Draw ytick labels to make sure they fit properly
for i in range(N):
angle_rad = i / float(N) * 2 * pi
if angle_rad == 0:
ha, distance_ax = "center", 10
elif 0 < angle_rad < pi:
ha, distance_ax = "left", 1
elif angle_rad == pi:
ha, distance_ax = "center", 1
else:
ha, distance_ax = "right", 1
ax.text(angle_rad, 100 + distance_ax, cat[i], size=10,
horizontalalignment=ha, verticalalignment="center")
animation.TimedAnimation.__init__(self, fig, interval=25, blit=True)
def new_frame_seq(self):
return iter(range(len(self.data)))
def _draw_frame(self, framedata):
ax.plot(ax, framedata)
testdata = [[10, 20, 30, 40, 50],
[10, 20, 30, 40, 50],
[40, 50, 60, 70, 80],
[40, 50, 60, 70, 80],
[50, 60, 70, 80, 90]]
ani = SubplotAnimation(testdata)
plt.show()
Any tips on how to make this work will be greatly appreciated!
It's not clear what the aim of subclassing TimedAnimation would be. It makes things much too complicated.
Here is a simple example of an animated radar plot using FuncAnimation.
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
fig = plt.figure(figsize=(4,4))
ax = fig.add_subplot(111, projection='polar')
ax.set_ylim(0,100)
data = np.random.rand(50)*6+2
theta = np.linspace(0,2.*np.pi, num=50)
l, = ax.plot([],[])
def update(i):
global data
data += (np.random.rand(50)+np.cos(i*2.*np.pi/50.))*2
data[-1] = data[0]
l.set_data(theta, data )
return l,
ani = animation.FuncAnimation(fig, update, frames=50, interval=200, blit=True)
plt.show()

How to plot bar graphs with same X coordinates side by side ('dodged')

import matplotlib.pyplot as plt
gridnumber = range(1,4)
b1 = plt.bar(gridnumber, [0.2, 0.3, 0.1], width=0.4,
label="Bar 1", align="center")
b2 = plt.bar(gridnumber, [0.3, 0.2, 0.2], color="red", width=0.4,
label="Bar 2", align="center")
plt.ylim([0,0.5])
plt.xlim([0,4])
plt.xticks(gridnumber)
plt.legend()
plt.show()
Currently b1 and b2 overlap each other. How do I plot them separately like so:
There is an example in the matplotlib site. Basically, you just shift the x values by width. Here is the relevant bit:
import numpy as np
import matplotlib.pyplot as plt
N = 5
menMeans = (20, 35, 30, 35, 27)
menStd = (2, 3, 4, 1, 2)
ind = np.arange(N) # the x locations for the groups
width = 0.35 # the width of the bars
fig = plt.figure()
ax = fig.add_subplot(111)
rects1 = ax.bar(ind, menMeans, width, color='royalblue', yerr=menStd)
womenMeans = (25, 32, 34, 20, 25)
womenStd = (3, 5, 2, 3, 3)
rects2 = ax.bar(ind+width, womenMeans, width, color='seagreen', yerr=womenStd)
# add some
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(ind + width / 2)
ax.set_xticklabels( ('G1', 'G2', 'G3', 'G4', 'G5') )
ax.legend( (rects1[0], rects2[0]), ('Men', 'Women') )
plt.show()
Below answer will explain each and every line of code in the simplest manner possible:
# Numbers of pairs of bars you want
N = 3
# Data on X-axis
# Specify the values of blue bars (height)
blue_bar = (23, 25, 17)
# Specify the values of orange bars (height)
orange_bar = (19, 18, 14)
# Position of bars on x-axis
ind = np.arange(N)
# Figure size
plt.figure(figsize=(10,5))
# Width of a bar
width = 0.3
# Plotting
plt.bar(ind, blue_bar , width, label='Blue bar label')
plt.bar(ind + width, orange_bar, width, label='Orange bar label')
plt.xlabel('Here goes x-axis label')
plt.ylabel('Here goes y-axis label')
plt.title('Here goes title of the plot')
# xticks()
# First argument - A list of positions at which ticks should be placed
# Second argument - A list of labels to place at the given locations
plt.xticks(ind + width / 2, ('Xtick1', 'Xtick3', 'Xtick3'))
# Finding the best position for legends and putting it
plt.legend(loc='best')
plt.show()
Sometimes could be tricky to find the right bar width. I usually use this np.diff to find the right dimension.
import numpy as np
import matplotlib.pyplot as plt
#The data
womenMeans = (25, 32, 34, 20, 25)
menMeans = (20, 35, 30, 35, 27)
indices = [5.5,6,7,8.5,8.9]
#Calculate optimal width
width = np.min(np.diff(indices))/3
fig = plt.figure()
ax = fig.add_subplot(111)
# matplotlib 3.0 you have to use align
ax.bar(indices-width,womenMeans,width,color='b',label='-Ymin',align='edge')
ax.bar(indices,menMeans,width,color='r',label='Ymax',align='edge')
ax.set_xlabel('Test histogram')
plt.show()
# matplotlib 2.0 (you could avoid using align)
# ax.bar(indices-width,womenMeans,width,color='b',label='-Ymin')
# ax.bar(indices,menMeans,width,color='r',label='Ymax')
This is the result:
What if my indices on my x axis are nominal values like names:
#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = (25, 32, 34, 20, 25)
menMeans = (20, 35, 30, 35, 27)
indices = range(len(womenMeans))
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min(np.diff(indices))/3.
fig = plt.figure()
ax = fig.add_subplot(111)
ax.bar(indices-width/2.,womenMeans,width,color='b',label='-Ymin')
ax.bar(indices+width/2.,menMeans,width,color='r',label='Ymax')
#tiks = ax.get_xticks().tolist()
ax.axes.set_xticklabels(names)
ax.set_xlabel('Test histogram')
plt.show()
Here are two examples of creating a side-by-side bar chart when you have more than two "categories" in a group.
Manual Method
Manually set the position and width of each bar.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import ticker
coins = ['penny', 'nickle', 'dime', 'quarter']
worth = np.array([.01, .05, .10, .25])
# Coin values times *n* coins
# This controls how many bars we get in each group
values = [worth*i for i in range(1,6)]
n = len(values) # Number of bars to plot
w = .15 # With of each column
x = np.arange(0, len(coins)) # Center position of group on x axis
for i, value in enumerate(values):
position = x + (w*(1-n)/2) + i*w
plt.bar(position, value, width=w, label=f'{i+1}x')
plt.xticks(x, coins);
plt.ylabel('Monetary Value')
plt.gca().yaxis.set_major_formatter(ticker.FormatStrFormatter('$%.2f'))
plt.legend()
Pandas Method
If you put the data into a pandas DataFrame, pandas will do the hard stuff for you.
import pandas as pd
coins = ['penny', 'nickle', 'dime', 'quarter']
worth = [0.01, 0.05, 0.10, 0.25]
df = pd.DataFrame(worth, columns=['1x'], index=coins)
df['2x'] = df['1x'] * 2
df['3x'] = df['1x'] * 3
df['4x'] = df['1x'] * 4
df['5x'] = df['1x'] * 5
from matplotlib import ticker
import matplotlib.pyplot as plt
df.plot(kind='bar')
plt.ylabel('Monetary Value')
plt.gca().yaxis.set_major_formatter(ticker.FormatStrFormatter('$%.2f'))
plt.gca().xaxis.set_tick_params(rotation=0)
Pandas creates a similar figure...

Categories