[PYTHON] Band graph with matplotlib

Since it is one shot in Excel, I tried to find out if matplotlib also has a band graph function, but it seems that it is not. Normalized and displayed so that the total value of the stacked bar chart is 1.

import numpy as np
import matplotlib.pyplot as plt

N, K = 4, 3
data = np.random.rand(N, K)
tick_labels = ["a", "b", "c", "d"]
labels = ["x", "y", "z"]

normalized = data / data.sum(axis=1, keepdims=True)
cumulative = np.zeros(N)
tick = np.arange(N)

for k in range(K):
    color = plt.cm.viridis(float(k) / K, 1)
    plt.barh(tick, normalized[:, k], left=cumulative, color=color, label=labels[k])
#   plt.bar(tick, normalized[:, k], bottom=cumulative, color=color, label=labels[k])
    cumulative += normalized[:, k]

plt.xlim((0, 1))
# plt.ylim((0, 1))
plt.yticks(tick, tick_labels)
# plt.xticks(tick, tick_labels)
plt.legend()
plt.show()
Bar chart

Reference: https://de.dariah.eu/tatom/topic_model_visualization.html

Recommended Posts

Band graph with matplotlib
Graph Excel data with matplotlib (1)
Graph drawing method with matplotlib
Graph Excel data with matplotlib (2)
Draw a loose graph with matplotlib
Animation with matplotlib
Japanese with matplotlib
matplotlib graph album
Animation with matplotlib
Histogram with matplotlib
Animate with matplotlib
Graph trigonometric functions with numpy and matplotlib
Create a graph with borders removed with matplotlib
Draw a flat surface with a matplotlib 3d graph
2-axis plot with Matplotlib
[Python] limit axis of 3D graph with Matplotlib
Read Python csv data with Pandas ⇒ Graph with Matplotlib
Graph drawing using matplotlib
Increase the font size of the graph with matplotlib
Heatmap with Python + matplotlib
Learn with Cheminformatics Matplotlib
Draw a graph with matplotlib from a csv file
Real-time drawing with matplotlib
The basis of graph theory with matplotlib animation
Graph drawing with jupyter (ipython notebook) + matplotlib + vagrant
Various colorbars with Matplotlib
3D plot with matplotlib
Align Matplotlib graph colors with similar colors (color map)
Adjust axes with matplotlib
Display the graph while changing the parameters with PySimpleGUI + Matplotlib
Draw a graph with NetworkX
Try using matplotlib with PyCharm
Stackable bar plot with matplotlib
Real-time graph display by matplotlib
Gradient color selection with matplotlib
Draw a graph with networkx
Animate multiple graphs with matplotlib
Graph drawing with IPython Notebook
Study math with Python: Draw a sympy (scipy) graph with matplotlib
Multiple file processing with Kivy + Matplotlib + Draw Graph on GUI
Create SVG graph with matplotlib on heroku (displayed in Japanese)
Create plot animation with Python + Matplotlib
A python graphing manual with Matplotlib.
Draw a graph with Julia + PyQtGraph (2)
Japaneseize Matplotlib with Alpine using Docker
[Python] font family and font with matplotlib
matplotlib: Insert comment on timeline graph
Learn with PyTorch Graph Convolutional Networks
Versatile data plotting with pandas + matplotlib
Draw a graph with Julia + PyQtGraph (1)
Draw a graph with Julia + PyQtGraph (3)
Output the call graph with PyCallGraph
Draw retention rate graph in Matplotlib
Heatmap with Dendrogram in Python + matplotlib
Draw a graph with pandas + XlsxWriter
Jupyter does not show matplotlib graph
Easy Japanese font setting with matplotlib
Let's make a graph with python! !!
Easy to draw graphs with matplotlib
Continuously color with matplotlib scatter plot
Make a nice graph with plotly