I'm new with python/matplotlib and I need some help adding bar headings to all my bars. Currently, this code creates a bar graph with a heading on only one bar in each series, I'm not sure what's going on and I greatly appreciate any help. The data is irrelevant, just trying to get the code ready.
from matplotlib import pyplot as plt
import numpy as np
plt.style.use(['science', 'no-latex'])
system_x = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
x_indexes = np.arange(len(system_x))
width = 0.2
cof_diamond = [1, 1, 2, 3, 1, 4, 2]
for i, v in enumerate(cof_diamond):
plt.text(x_indexes[i] - 0.35, v + 0.05, str(v), color='black')
plt.bar(x_indexes - width, cof_diamond, label='diamond', color='crimson', width=width)
cof_3000 = [1, 1, 2, 3, 1, 4, 2]
for i, v in enumerate(cof_3000):
plt.text(x_indexes[i] - 0.35, v + 0.05, str(v), color='black')
plt.bar(x_indexes, cof_3000, label='$ta-C_{3000K}$', color='slategrey', width=width)
cof_4000 = [1, 1, 2, 3, 1, 4, 2]
for i, v in enumerate(cof_4000):
plt.text(x_indexes[i] - 0.35, v + 0.05, str(v), color='black')
plt.bar(x_indexes + width, cof_4000, label='$ta-C_{4000K}$', color='orange', width=width)
plt.xticks(ticks=x_indexes, labels=system_x)
plt.xlabel('System Type')
plt.ylabel('CoF')
plt.title('Mean CoF')
leg = plt.legend()
leg_lines = leg.get_lines()
leg_texts = leg.get_texts()
plt.setp(leg_lines, linewidth=4)
plt.grid(False)
plt.tight_layout()
plt.show()
All three of your plt.text calls have the same X coordinates.
Change them to
plt.text(x_indexes[i] - 0.35, v + 0.05, str(v), color='black')
...
plt.text(x_indexes[i], v + 0.05, str(v), color='black')
...
plt.text(x_indexes[i] + 0.35, v + 0.05, str(v), color='black')
will fix it. You can change 0.35 to a more appropriate number to make it look prettier.
I'd suggest using the method here:
system_x = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
x_indexes = np.arange(len(system_x))
width = 0.2
fig, ax = plt.subplots()
cof_diamond = [1, 1, 2, 3, 1, 4, 2]
r0 = ax.bar(x_indexes - width, cof_diamond, label='diamond', color='crimson', width=width)
cof_3000 = [2, 1, 2, 3, 1, 3, 2]
r1 = ax.bar(x_indexes, cof_3000, label='$ta-C_{3000K}$', color='slategrey', width=width)
cof_4000 = [1, 1, 2, 3, 1, 2, 2]
r2 = ax.bar(x_indexes + width, cof_4000, label='$ta-C_{4000K}$', color='orange', width=width)
def autolabel(rects):
"""Attach a text label above each bar in *rects*, displaying its height."""
for rect in rects:
height = rect.get_height()
ax.annotate('{}'.format(height),
xy=(rect.get_x() + rect.get_width() / 2, height),
xytext=(0, 3), # 3 points vertical offset
textcoords="offset points",
ha='center', va='bottom')
autolabel(r0)
autolabel(r1)
autolabel(r2)
plt.xticks(ticks=x_indexes, labels=system_x)
plt.xlabel('System Type')
plt.ylabel('CoF')
plt.title('Mean CoF')
leg = plt.legend()
leg_lines = leg.get_lines()
leg_texts = leg.get_texts()
plt.setp(leg_lines, linewidth=4)
plt.grid(False)
plt.tight_layout()
The exellent example you posted made this very easy to answer. Thanks! For the future, both for posting here and debugging on your own, if you make the example with unusual and non-identical numbers you will generally find your errors more quickly. Here's your example, but with different numbers:
Related
How can this vertical grouped bar chart be changed to a horizontal bar chart (grouped, and stacked)? I need help to alter the code such that the bars are displayed horizontally instead of vertically.
import matplotlib.pyplot as plt
import numpy as np
N = 9
labels = ['L', 'S', 'S', 'M', 'W', 'W', 'S', 'R', 'C']
M_means = [1, 45, 28, 11, 4, 7, 1, 0.02, 0.3]
PO_means = [3, 58, 17, 8, 3, 8, 1, 0.06, 1]
K_means = [1, 44, 30, 11, 3, 7, 1, 0.01, 0.5]
x = np.arange(len(labels)) # the label locations
width = 0.30 # the width of the bars
fig, ax = plt.subplots(figsize=(15, 9))
rects1 = ax.bar(x - width, M_means, width, label='M S and K', color=('#b02a2a'))
rects2 = ax.bar(x, PO_means, width, label='P O S and K', color=('#055cad'))
rects3 = ax.bar(x + width, K_means, width, label='M K', color=('#0b7d53'))
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('% of workday', fontsize=32)
#ax.set_title('Scores by group and gender')
ax.set_xticks(x)
ax.set_xticklabels(labels, fontsize=32, rotation=15)
ax.legend(loc='upper right', frameon=False, fontsize=32, markerscale=2)
ax.bar_label(rects1, size = 32, padding=20, rotation=90)
ax.bar_label(rects2, size = 32, padding=20, rotation=90)
ax.bar_label(rects3, size = 32, padding=20, rotation=90)
plt.xticks(ha='center')
for tick in ax.xaxis.get_major_ticks():
tick.label.set_fontsize(32)
for tick in ax.yaxis.get_major_ticks():
tick.label.set_fontsize(32)
plt.ylim(0, 100)
plt.gca().spines['right'].set_color('none')
plt.gca().spines['top'].set_color('none')
#fig.tight_layout()
plt.show()
Functionally, only two changes are needed:
Change ax.bar to ax.barh
Swap set_x* methods with set_y* methods, e.g. set_xticks() -> set_yticks() and so on
Semantically, the variables x and width should also be renamed to y and height.
import matplotlib.pyplot as plt
import numpy as np
N = 9
labels = list('LSSMWWSRC')
M_means = [1, 45, 28, 11, 4, 7, 1, 0.02, 0.3]
K_means = [2, 40, 21, 18, 3, 3, 2, 0.52, 0.3]
PO_means = [3, 58, 17, 8, 3, 8, 1, 0.06, 1]
K = [1, 44, 30, 11, 3, 7, 1, 0.01, 0.5]
# rename x/width to y/height
y = np.arange(len(labels))
height = 0.30
fig, ax = plt.subplots()
# use ax.barh instead of ax.bar
rects1 = ax.barh(y - height, M_means, height, label='M S and K', color='#b02a2a')
rects2 = ax.barh(y, PO_means, height, label='P O S and K', color='#055cad')
rects3 = ax.barh(y + height, K_means, height, label='M K', color='#0b7d53')
# swap set_x* methods with set_y* methods
ax.set_xlabel('% of workday')
ax.set_yticks(y)
ax.set_yticklabels(labels)
ax.legend(loc='upper right', frameon=False, markerscale=2)
ax.bar_label(rects1, padding=10)
ax.bar_label(rects2, padding=10)
ax.bar_label(rects3, padding=10)
# ...
The easiest solution is to load the data into a pandas.DataFrame, and then use pandas.DataFrame.plot with kind='barh'. This is easier because pandas uses matplotlib as the default plotting backend, and the API groups the bars automatically.
This reduces the code to 14 lines (not including imports).
When using 'barh', xlabel= applies to the y-axis. Therefore, xlabel='' removes the y-axis label.
Adjust figsize=(12, 10) if planning to use smaller / larger font sizes.
See Adding value labels on a matplotlib bar chart for additional details about using .bar_label.
Tested in python 3.10, pandas 1.4.2, matplotlib 3.5.1
import pandas as pd
import matplotlib.pylot as plt
# data
labels = ['L', 'S', 'S', 'M', 'W', 'W', 'S', 'R', 'C']
M_means = [1, 45, 28, 11, 4, 7, 1, 0.02, 0.3]
PO_means = [3, 58, 17, 8, 3, 8, 1, 0.06, 1]
K_means = [1, 44, 30, 11, 3, 7, 1, 0.01, 0.5]
# create a dict with the keys as the desired legend labels
data = {'labels': labels, 'M S and K': M_means, 'P O S and K': PO_means, 'M K': K_means}
# create dataframe
df = pd.DataFrame(data)
# plot: specify y=[...] if only certain columns are desired
ax = df.plot(kind='barh', x='labels', width=.85, figsize=(12, 10), xlabel='', color=['#b02a2a', '#055cad', '#0b7d53'])
ax.set_xlabel('% of workday', fontsize=15)
ax.set_xlim(0, 100)
ax.legend(loc='upper right', frameon=False, fontsize=15, markerscale=2)
for c in ax.containers:
ax.bar_label(c, label_type='edge', padding=1, size=15)
ax.tick_params(axis='both', which='both', labelsize=15)
ax.spines[['top', 'right']].set_visible(False)
Stacked
To manually create the stacked bar without pandas, see Horizontal stacked bar chart in Matplotlib
Use the parameter stacked=True
Some bar patches are to small for the label, so custom labels have been passed to the labels= parameter in .bar_label
Using := requires at least python 3.8. Otherwise use labels = [f'{v.get_width():.0f}' if v.get_width() > 1 else '' for v in c]
ax = df.plot(kind='barh', x='labels', width=.85, figsize=(12, 10), xlabel='',
color=['#b02a2a', '#055cad', '#0b7d53'], stacked=True)
ax.set_xlabel('% of workday', fontsize=15)
ax.set_xlim(0, 100)
ax.legend(loc='upper right', frameon=False, fontsize=15, markerscale=2)
for c in ax.containers:
# custom labels only show label size for values greater than 1
labels = [f'{w:.0f}' if (w := v.get_width()) > 1 else '' for v in c]
ax.bar_label(c, labels=labels, label_type='center', padding=1, size=15)
ax.tick_params(axis='both', which='both', labelsize=15)
ax.spines[['top', 'right']].set_visible(False)
I'm creating a stacked horizontal bar graph with 3 segments using the code below:
import matplotlib.pyplot as plt
import numpy as np
def create_stacked_hbar(data):
fig, ax = plt.subplots(figsize=(10, 10))
ylabels = list(data.keys())
labels = ['a', 'b', 'c', 'd', 'e', 'f']
c = []
v = []
for key, val in data.items():
c.append(key)
v.append(val)
v = np.array(v)
print(v)
plt.barh(range(len(c)), v[:,0], width=1, color='red',
edgecolor='w',linewidth=2, tick_label=ylabels, label=labels[0])
plt.barh(range(len(c)), v[:,1], width=1, left=v[:,0], color='orange',
edgecolor='w', linewidth=2, label=labels[1])
plt.barh(range(len(c)), v[:,2], width=1, left=(v[:,0]+v[:,1]), color='yellow',
edgecolor='w', linewidth=2, label=labels[2])
for p in ax.patches:
left, bottom, width, height = p.get_bbox().bounds
if width != 0.0:
ax.annotate(str(int(width)), xy=(left+width/2, bottom+height/2),
ha='center', va='center', size = 12)
plt.legend(bbox_to_anchor=(0, -0.15), loc=3, prop={'size': 14}, frameon=False)
plt.yticks(np.arange(len(ylabels)), ylabels)
plt.show()
data = {'A': [8, 7, 2], 'B': [0, 2, 0],
'C': [3, 2, 4], 'D': [0, 4, 0],
'E': [0, 1, 1], 'F': [0, 1, 0],
'G': [0, 0, 0]}
create_stacked_hbar(data)
The issue is that in attempting to set width = 1 in the bars throws a type error:
TypeError: <lambda>() got multiple values for argument 'width'
removing width allows to the code to work, but I do need to increase the width of the bars in the chart. I suspect this has to do with the annotation code I use in this case. Does anyone have any suggestions on getting around this?
Also note I am unable to use the "dataframe.plot.barh(data, stacked=True)" method via pandas to generate this chart.
You are making a horizontal bar plot, the width parameter corresponds to the data, so in your example you are passing both v[:,0] and 1 as width. If you are trying to specify the height because you do not desire whitespace between the bars you need to set height=1, consider this example:
import numpy as np
import matplotlib.pyplot as plt
# Seeded for reproducing
np.random.seed(1)
v1 = abs(np.random.randn(10))
v2 = abs(np.random.randn(10))
v3 = abs(np.random.randn(10))
c = range(10)
plt.title("Sample bar plot")
plt.barh(c, v1, height=1, alpha=0.8, color='r')
plt.barh(c, v2, height=1, left=v1, alpha=0.8, color='b')
plt.barh(c, v3, height=1, left=v1+v2, alpha=0.8, color='g')
plt.show()
This will give you
Where removing the height=1 specification would give you
I have two dictionaries which's values are the number of people...
d_1={1947: 1, 1950: 1, 1951: 2, 1955: 2, 1956: 1, 1957: 2, 1958: 2, 1959: 3, 1960: 1....}
d_2={1936: 1, 1945: 1, 1948: 2, 1949: 1, 1950: 2, 1951: 2, 1952: 3, 1953: 1, 1954: 41..}
And i want to create bar plot that displays the number of people . And the X-axis is formed in year .
When I used this code format is almost the same except the stacked bars. I want them to be seperated.How can I fix that?
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt; plt.rcdefaults()
x1=[t for t in d_1.keys()]
y1=[k for k in d_1.values()]
x2=[m for m in d_2.keys()]
y2=[n for n in d_2.values()]
fig, ax = plt.subplots()
index = np.arange(len(years_without_duplicates))
#list which has the years without duplicates
bar_width = 0.35
rects1 = plt.bar(x1, y1, bar_width,color='b')
rects2 = plt.bar(x2, y2, bar_width, color='r')
plt.xlabel('Years')
plt.ylabel('number of people')
plt.xticks([i for i in range(min(years_without_duplicates),max(years_without_duplicates)+1)],rotation=80,fontsize=6)
plt.legend()
plt.tight_layout()
plt.show()
Just shift the bars:
bar_width = 0.35
x1 = [t - bar_width / 2 for t in d_1.keys()]
y1 = [k for k in d_1.values()]
x2 = [m + bar_width / 2 for m in d_2.keys()]
y2 = [n for n in d_2.values()]
I am trying to display two polar plots in one window using matplotlib. This is realized using subplots. Each subplot is created using this solution. The two diagrams are then combined using this solution:
The Radar class handles the creation of a single radar chart:
class Radar:
def __init__(self, fig, titles, labels, ylimit, lines, rect=None):
if rect is None:
rect = [0.2, 0.2, 0.6, 0.6]
self.n = len(titles)
self.angles = np.arange(90, 90 + 360, 360.0 / self.n)
self.axes = [fig.add_axes(rect, projection="polar", label="axes%d" % i) for i in range(self.n)]
self.ax = self.axes[0]
self.ax.set_thetagrids(self.angles, labels=titles, fontsize=14)
for ax in self.axes[1:]:
ax.patch.set_visible(False)
ax.grid("off")
ax.xaxis.set_visible(False)
for ax, angle, label in zip(self.axes, self.angles, labels):
ax.set_rgrids(lines, angle=angle, labels=label)
ax.spines["polar"].set_visible(False)
ax.set_ylim(ylimit[0], ylimit[1])
def plot(self, values, *args, **kw):
angle = np.deg2rad(np.r_[self.angles, self.angles[0]])
values = np.r_[values, values[0]]
return self.ax.plot(angle, values, *args, **kw)
The following code is used in order to create two radar charts and add them to one figure:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.pyplot import Line2D
fig1 = plt.figure(figsize=(9, 9))
plt.ioff()
#############################
# first radar chart
#############################
titles = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
lbl_count = 7
upper_bound = 70
values = [0, 10, 40, 30, 20, 50, 30, 40]
labels1 = np.tile(np.arange(-60 + upper_bound / lbl_count, 20, upper_bound / lbl_count), (8, 1))
lines1 = np.arange(10, upper_bound, 10)
radar1 = Radar(fig1, titles, labels1, (0, upper_bound), lines1)
plt1 = radar1.plot(values, "-", lw=2, color="b", alpha=0.4, label="Fitness") # type: List[Line2D]
#############################
# second radar chart
#############################
fig2 = plt.figure(figsize=(9, 9))
values = [0.4, 0.7, 0.2, 0.1, 0.8, 0.3, 0.5, 0.7]
lbl_count = 5
labels2 = [list("12345"), [0.1, 0.2, 0.3, 0.4, 0.5], list("54321"), [10, 8, 6, 4, 2], list("12345"), list("12345"), list("12345"), list("12345")]
lines2 = np.arange(0.2, 1.2, 0.2)
radar2 = Radar(fig2, titles, labels2, (0, 1), lines2)
plt2 = radar2.plot(values, "-", lw=2, color="b", alpha=0.4, label="Values")
plt3 = radar2.plot([0.1, 0.2, 0.5, 0.2, 0.1, 0.7, 0.4, 0.2], "-", lw=2, color="r", alpha=0.4, label="Critical Thresholds")
#############################
# combine radar charts
#############################
fig3, (ax1, ax2) = plt.subplots(1, 2, subplot_kw=dict(projection='polar'), figsize=(25, 15))
line1, = ax1.plot(plt1[0].get_xdata(), plt1[0].get_ydata(), 'g-', label="Fitness")
line2, = ax2.plot(plt2[0].get_xdata(), plt2[0].get_ydata(), 'b-', label="Values")
line3, = ax2.plot(plt3[0].get_xdata(), plt3[0].get_ydata(), 'r-', label="Critical Thresholds")
ax1.set_ylim(0, 80)
ax2.set_ylim(0, 1)
plt.tight_layout()
plt.show()
plt.close()
After combining the two figures, the labels with the different scales are gone (figures 1 and 2 are the desired result, while the combined figure 3 is missing some lables)
How do I add the missing labels?
You need to actually use the radar class if you want to benefit from its features.
fig3 = plt.figure(figsize=(13, 8))
titles = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
### first subplot:
lbl_count = 7
upper_bound = 70
values = [0, 10, 40, 30, 20, 50, 30, 40]
labels1 = np.tile(np.arange(-60 + upper_bound / lbl_count, 20, upper_bound / lbl_count), (8, 1))
lines1 = np.arange(10, upper_bound, 10)
radar1 = Radar(fig3, titles, labels1, (0, upper_bound), lines1, rect=[0.55,0.1,0.35,0.8])
plt1 = radar1.plot(values, "-", lw=2, color="b", alpha=0.4, label="Fitness")
### second subplot:
values = [0.4, 0.7, 0.2, 0.1, 0.8, 0.3, 0.5, 0.7]
lbl_count = 5
labels2 = [list("12345"), [0.1, 0.2, 0.3, 0.4, 0.5], list("54321"), [10, 8, 6, 4, 2], list("12345"), list("12345"), list("12345"), list("12345")]
lines2 = np.arange(0.2, 1.2, 0.2)
radar2 = Radar(fig3, titles, labels2, (0, 1), lines2, rect=[0.1,0.1,0.35,0.8])
plt2 = radar2.plot(values, "-", lw=2, color="b", alpha=0.4, label="Values")
plt3 = radar2.plot([0.1, 0.2, 0.5, 0.2, 0.1, 0.7, 0.4, 0.2], "-", lw=2, color="r", alpha=0.4, label="Critical Thresholds")
plt.show()
I cannot seem to make the plots work with labels correctly. The plots work in terms of generating three sub plots bar charts. But what I want to label each and every plot (3) with labels cr_lst. How do I ensure that I can label each of these bars with cr_lst and on each bar.
plt.figure(0)
width = 0.35 # the width of the bars
cr_lst = ['A', 'B', 'C', 'D']
A_lst = [1, 2, 3, 4]
B_lst = [2, 2, 6, 7]
A_lst = [8, 8, 6, 7]
ind = np.arange(len(A_lst)) # the x locations for the groups
f, axarr = plt.subplots(3, sharex=True)
axarr[0].set_title('Three plots\n')
axarr[0].set_ylabel('A')
axarr[1].set_ylabel('B')
axarr[2].set_ylabel('C')
axarr[0].set_ylim(ymin=0.001,ymax=max(A_lst)*1.10)
axarr[1].set_ylim(ymin=0.001,ymax=max(B_lst)*1.10)
axarr[2].set_ylim(ymin=0.001,ymax=max(B_lst)*1.10)
axarr[0].grid()
axarr[1].grid()
axarr[2].grid()
rects1 = axarr[0].bar(ind, A_lst, width, color='r', linewidth=1,alpha=0.8, label=cr_lst)
rects2 = axarr[1].bar(ind, B_lst, width, color='y', linewidth=1,alpha=0.8, label=cr_lst)
rects3 = axarr[2].bar(ind, C_lst, width, color='blue', linewidth=1, alpha=0.8, label=cr_lst)
plt.savefig("ByC.png")
I'd like to have the labels shown on the x-axis.
This will get you the labels under each bar on every axes:
width = 0.35 # the width of the bars
cr_lst = ['A', 'B', 'C', 'D']
x = range(len(cr_lst)) # the x locations for the groups
A_lst = [1, 2, 3, 4]
B_lst = [2, 2, 6, 7]
C_lst = [8, 8, 6, 7]
f, axarr = plt.subplots(3, sharex=False)
axarr[0].set_title('Three plots\n')
axarr[0].set_ylabel('A')
axarr[1].set_ylabel('B')
axarr[2].set_ylabel('C')
axarr[0].set_ylim(ymin=0.001,ymax=max(A_lst)*1.10)
axarr[1].set_ylim(ymin=0.001,ymax=max(B_lst)*1.10)
axarr[2].set_ylim(ymin=0.001,ymax=max(B_lst)*1.10)
axarr[0].grid()
axarr[1].grid()
axarr[2].grid()
rects1 = axarr[0].bar(x, A_lst, width, color='r', align='center', linewidth=1,alpha=0.8)
rects2 = axarr[1].bar(x, B_lst, width, color='y', align='center', linewidth=1,alpha=0.8)
rects3 = axarr[2].bar(x, C_lst, width, color='blue', align='center', linewidth=1, alpha=0.8)
for ax in axarr:
ax.set_xticks(x)
ax.set_xticklabels(cr_lst)
plt.savefig("ByC.png")
Note that share=False in plt.subplots. If you set it to True it hides all other labels but the lowest ax.
Also note the use of align='center' in .bar().
This yields: