Horizontal stacked bar chart python
To plot stacked bar chart in Matplotlib, we can use barh() methods Show Steps
Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True year = [2014, 2015, 2016, 2017, 2018, 2019] issues_addressed = [10, 14, 0, 10, 15, 15] issues_pending = [5, 10, 50, 2, 0, 10] b1 = plt.barh(year, issues_addressed, color="red") b2 = plt.barh(year, issues_pending, left=issues_addressed, color="yellow") plt.legend([b1, b2], ["Completed", "Pending"], title="Issues", loc="upper right") plt.show() Output
Updated on 15-Jun-2021 12:25:28
I'm trying to create a horizontal stacked bar chart using Here's my testing code.
Edited to use
This seems to be the right approach, but it fails if there is no data for a particular bar as it's trying to add Note Click here to download the full example code Stacked bar
charts can be used to visualize discrete distributions. This example visualizes the result of a survey in which people could rate their agreement to questions on a five-element scale. The horizontal stacking is achieved by calling import numpy as np import matplotlib.pyplot as plt category_names = ['Strongly disagree', 'Disagree', 'Neither agree nor disagree', 'Agree', 'Strongly agree'] results = { 'Question 1': [10, 15, 17, 32, 26], 'Question 2': [26, 22, 29, 10, 13], 'Question 3': [35, 37, 7, 2, 19], 'Question 4': [32, 11, 9, 15, 33], 'Question 5': [21, 29, 5, 5, 40], 'Question 6': [8, 19, 5, 30, 38] } def survey(results, category_names): """ Parameters ---------- results : dict A mapping from question labels to a list of answers per category. It is assumed all lists contain the same number of entries and that it matches the length of *category_names*. category_names : list of str The category labels. """ labels = list(results.keys()) data = np.array(list(results.values())) data_cum = data.cumsum(axis=1) category_colors = plt.colormaps['RdYlGn']( np.linspace(0.15, 0.85, data.shape[1])) fig, ax = plt.subplots(figsize=(9.2, 5)) ax.invert_yaxis() ax.xaxis.set_visible(False) ax.set_xlim(0, np.sum(data, axis=1).max()) for i, (colname, color) in enumerate(zip(category_names, category_colors)): widths = data[:, i] starts = data_cum[:, i] - widths rects = ax.barh(labels, widths, left=starts, height=0.5, label=colname, color=color) r, g, b, _ = color text_color = 'white' if r * g * b < 0.5 else 'darkgrey' ax.bar_label(rects, label_type='center', color=text_color) ax.legend(ncol=len(category_names), bbox_to_anchor=(0, 1), loc='lower left', fontsize='small') return fig, ax survey(results, category_names) plt.show() Gallery generated by Sphinx-Gallery How do you plot a horizontal stacked bar graph in Python?Steps. Set the figure size and adjust the padding between and around the subplots. Create a list of years, issues_addressed and issues_pending, in accordance with years. Plot horizontal bars with years and issues_addressed data.
How do you create a stack bar in Python?Matplotlib is a tremendous visualization library in Python for 2D plots of arrays. Matplotlib may be a multi-platform data visualization library built on NumPy arrays and designed to figure with the broader SciPy stack.. Import Library (Matplotlib). Import / create data.. Plot the bars in the stack manner.. How do I show values in a stacked bar chart in Matplotlib?DataFrame. plot(kind='bar', stacked=True) , is the easiest way to plot a stacked bar plot. This method returns a matplotlib.
|