[PYTHON] Adjust the ratio of multiple figures with the matplotlib gridspec

Return to list

When grasping trends in samples and time series data, it is convenient to plot a scatter plot (+ solid line) on the left side and a histogram on the right side as shown below. In such a case, use gridspec to adjust the width of the graph with matplotlib. If you want to add a histogram on the upper side as well, it is convenient to use seaborn jointtplot.

image.png

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
%matplotlib inline
plt.rcParams['font.size'] = 15

x = np.random.randn(1000)

gs = gridspec.GridSpec(1, 2, width_ratios=[4,1], height_ratios=[1] ,wspace=0.05)

plt.figure(figsize=(8,3))
plt.subplot(gs[0])
plt.scatter(range(len(x)),x,color='k',s=2)
plt.ylim(-4,4)
plt.xlabel('index')
plt.ylabel('x')
plt.axhline(y=0,color='gray',lw=1)

plt.subplot(gs[1])
plt.hist(x,orientation='horizontal',bins=20,color='k')
plt.tick_params(left=False,labelleft=False)
plt.ylim(-4,4)
plt.xlabel('freq.')
plt.axhline(y=0,color='gray',lw=1)

plt.show()

Recommended Posts

Adjust the ratio of multiple figures with the matplotlib gridspec
Adjust the spacing between figures with Matplotlib
How to title multiple figures with matplotlib
Align the size of the colorbar with matplotlib
Adjust the bin width crisply and neatly with the histogram of matplotlib and seaborn
Increase the font size of the graph with matplotlib
The basis of graph theory with matplotlib animation
Visualize the behavior of the sorting algorithm with matplotlib
Adjust axes with matplotlib
Get the sum of each of multiple columns with awk
The story of sharing the pyenv environment with multiple users
Change the style of matplotlib
Animate multiple graphs with matplotlib
Let's visualize the number of people infected with coronavirus with matplotlib
Unravel the mystery of matplotlib specgram
About the size of matplotlib points
Plot multiple maps and data at the same time with Python's matplotlib
[Python] Set the graph range with matplotlib
[pyqtgraph] Set the size ratio of the graph
Check the existence of the file with python
Set the xticklabels color individually with matplotlib
The third night of the loop with for
Display markers above the border with matplotlib
The second night of the loop with for
Match the colorbar to the figure with matplotlib
Count the number of characters with echo
Make common settings with subplot of matplotlib
Type conversion of multiple columns of pandas DataFrame with astype at the same time
How to assign multiple values to the Matplotlib colorbar
The story of doing deep learning with TPU
Note: Prepare the environment of CmdStanPy with docker
Prepare the execution environment of Python3 with Docker
About the X-axis notation of Matplotlib bar graphs
[Python] limit axis of 3D graph with Matplotlib
2016 The University of Tokyo Mathematics Solved with Python
Drawing tips with matplotlib on the server side
Draw multiple graphs using matplotlib figures and axes
[Note] Export the html of the site with python.
See the behavior of drunkenness with reinforcement learning
I want to display multiple images with matplotlib.
Calculate the total number of combinations with python
[Python] How to draw multiple graphs with Matplotlib
Adjust file permissions with the Linux command chmod
Use multiple versions of python environment with pyenv
Check the date of the flag duty with Python
One liner that lists the colors of matplotlib
Eliminate the inconveniences of QDock Widget with PySide
Rewrite the name of the namespaced tag with lxml
Predict the second round of summer 2016 with scikit-learn
Dump the contents of redis db with lua
Tucker decomposition of the hay process with HOOI
Put the second axis in 2dhistgram of matplotlib
Find out the day of the week with datetime
Code for checking the operation of Python Matplotlib
[Python] Determine the type of iris with SVM
matplotlib: Replace the axis itself with another one.
Try to create a battle record table with matplotlib from the data of "Schedule-kun"