How to draw a bar graph that summarizes multiple series with matplotlib

As a memorandum because there was no good article.

# version
matplotlib == 3.2.2

import numpy as np
import matplotlib.pyplot as plt

#Vertical bar
def barplot(ax, labels, datas):
    x = np.arange(len(labels))  # the label locations
    width = 0.35  # the width of the bars

    rects1 = ax.bar(x - width/2, datas[0]["val"], width, label=datas[0]["label"])
    rects2 = ax.bar(x + width/2, datas[1]["val"], width, label=datas[1]["label"])

    # Add some text for labels, title and custom x-axis tick labels, etc.
    ax.set_xticks(x)
    ax.set_xticklabels(labels)

#Horizontal bar
def barhplot(ax, labels, datas):
    x = np.arange(len(labels))  # the label locations
    width = 0.35  # the width of the bars

    rects1 = ax.barh(x - width/2, datas[0]["val"], width, label=datas[0]["label"])
    rects2 = ax.barh(x + width/2, datas[1]["val"], width, label=datas[1]["label"])

    # Add some text for labels, title and custom x-axis tick labels, etc.
    ax.set_yticks(x)
    ax.set_yticklabels(labels)
    plt.gca().invert_yaxis()

The usage is as follows.


labels = ['G1', 'G2', 'G3', 'G4', 'G5']
datas = [
    {"label": "men1", "val": [20, 34, 30, 35, 27]},
    {"label": "women2", "val": [25, 32, 34, 20, 25]}]
fig, ax = plt.subplots()
# barhplot(ax, labels, datas)
barplot(ax, labels, datas)
plt.legend()
plt.show()

In the case of 3 or more, it is necessary to adjust the width and arrange them.

Recommended Posts

How to draw a bar graph that summarizes multiple series with matplotlib
[Python] How to draw a line graph with Matplotlib
How to draw a graph using Matplotlib
How to draw a 2-axis graph with pyplot
[Python] How to draw multiple graphs with Matplotlib
[Graph drawing] I tried to write a bar graph of multiple series with matplotlib and seaborn
[Python] How to draw a scatter plot with Matplotlib
Draw a loose graph with matplotlib
How to title multiple figures with matplotlib
Draw a flat surface with a matplotlib 3d graph
How to draw a 3D graph before optimization
(Matplotlib) I want to draw a graph with a size specified in pixels
Draw a graph with matplotlib from a csv file
[Python] How to draw a histogram in Matplotlib
[Python] How to create a 2D histogram with Matplotlib
Draw a graph with NetworkX
Draw a graph with networkx
Study math with Python: Draw a sympy (scipy) graph with matplotlib
How to build a python2.7 series development environment with Vagrant
[Visualization] I want to draw a beautiful graph with Plotly
Draw a graph with Julia + PyQtGraph (2)
Draw a graph with Julia + PyQtGraph (1)
Draw a graph with Julia + PyQtGraph (3)
Draw a graph with pandas + XlsxWriter
Easy to draw graphs with matplotlib
Draw a graph with PySimple GUI
How to plot a lot of legends by changing the color of the graph continuously with matplotlib
Create a poster with matplotlib to visualize multiplication tables that remember multiplication
How to add a package with PyCharm
matplotlib Write text to time series graph
Draw a graph with PyQtGraph Part 1-Drawing
Create a graph with borders removed with matplotlib
How to call a POST request that supports Japanese (Shift-JIS) with requests
[Small story] How to save matplotlib graphs in a batch with Jupyter
How to draw a vertical line on a heatmap drawn with Python seaborn
Draw a graph that can be moved around with HoloViews and Bokeh
How to assign multiple values to the Matplotlib colorbar
A memo for making a figure that can be posted to a journal with matplotlib
How to read a CSV file with Python 2/3
How to send a message to LINE with curl
Draw a graph with Japanese labels in Jupyter
How to Skip Correlation Graph Depiction with pandas-profiling
A memo that made a graph animated with plotly
Draw a graph by processing with Pandas groupby
If you don't know how to draw the graph you want with matplotlib, it is convenient to look at the gallery.
[Python] Draw a directed graph with Dash Cytoscape
Try to draw a life curve with python
I want to display multiple images with matplotlib.
How to make a dictionary with a hierarchical structure.
Create a stacked graph corresponding to both positive and negative directions with matplotlib
Draw a graph with PyQtGraph Part 4-PlotItem settings
How to display images continuously with matplotlib Note
How to create a multi-platform app with kivy
Draw a graph with PyQtGraph Part 6-Displaying a legend
Draw multiple photos in a graph from multiple folders
How to return multiple indexes with index method
A new form of app that works with GitHub: How to make GitHub Apps
I created a stacked bar graph with matplotlib in Python and added a data label
Draw a graph with PyQtGraph Part 5-Increase the Y-axis
How to convert / restore a string with [] in python
How to create a submenu with the [Blender] plugin