Convenient to use matplotlib subplots in a for statement

** Be careful not to confuse subplot with subplots **

Convenient to use subplots in a for statement

import numpy as np
import matplotlib.pyplot as plt
plt.style.use('seaborn-whitegrid')
%matplotlib inline

#Prepare the data
t = np.linspace(-np.pi, np.pi, 1000)
x1 = np.sin(2*t)
x2 = np.cos(2*t)
x3 = np.tan(2*t)
x4 = 1 / x1
x5 = 1 / x2
x6 = 1 / x3

-(Example 1) Plot 6 data in 2 rows and 3 columns

fig, axes = plt.subplots(2, 3, figsize=(16,12))
l = [[x1, x2, x3], [x4, x5, x6]]
for i in range(2):
    for j in range(3):
        axes[i][j].plot(t, l[i][j])
        axes[i][j].set_xlim(-np.pi, np.pi)
        axes[i][j].set_ylim(-1, 1)

tmp2.png

-(Example 2) Plot 6 data in 3 rows and 2 columns

fig, axes = plt.subplots(3, 2, figsize=(8,12))
l = [[x1, x2], [x3, x4], [x5, x6]]
for i in range(3):
    for j in range(2):
        axes[i][j].plot(t, l[i][j], color='r')
        axes[i][j].set_xlim(-np.pi, np.pi)
        axes[i][j].set_ylim(-1, 1)

tmp.png

Brief commentary

--The first and second arguments of subplots are the rows and columns of the graph. --fig size is the size per graph (inch) --Use axes and l (list of data you want to visualize) as if you were slicing

Recommended Posts

Convenient to use matplotlib subplots in a for statement
[Introduction to Python] How to use the in operator in a for statement?
When you want to plt.save in a for statement
How to define multiple variables in a python for statement
Change the list in a for statement
Use a scripting language for a comfortable C ++ life 3-Leave graphing to matplotlib-
[Python] It was very convenient to use a Python class for a ROS program.
How to write a ShellScript Bash for statement
Don't use readlines () in your Python for statement!
[Python] How to draw a histogram in Matplotlib
How to use Matplotlib
Use libsixel to output Sixel in Python and output a Matplotlib graph to the terminal.
Create a dataset of images to use for learning
[For beginners] How to use say command in python!
A memorandum on how to use keras.preprocessing.image in Keras
I thought it would be slow to use a for statement in NumPy, but that wasn't the case.
[Python] I want to use only index when looping a list with a for statement
[python] How to use the library Matplotlib for drawing graphs
How to use the __call__ method in a Python class
A simple way to avoid multiple for loops in Python
Use MeCab to translate sloppy sentences in a "slow" way.
I didn't know how to use the [python] for statement
Use a scripting language for a comfortable C ++ life-OpenCV-Port Python to C ++-
It may be a problem to use Japanese for folder names and notebook names in Databricks
How to use classes in Theano
Mock in python-how to use mox
[Introduction to Python] How to output a character string in a Print statement
How to use SQLite in Python
[Selenium] Use a while statement to repeatedly move to the "next page"
[For beginners] How to register a library created in Python in PyPI
How to use Mysql in python
How to use ChemSpider in Python
How to use PubChem in Python
[Python] How to use the for statement. A method of extracting by specifying a range or conditions.
A memo for those who use Python in Visual Studio (me)
I want to use complicated four arithmetic operations in the IF statement of the Django template! → Use a custom template
I made a Docker container to use JUMAN ++, KNP, python (for pyKNP).
[Small story] How to save matplotlib graphs in a batch with Jupyter
How to make a model for object detection using YOLO in 3 hours
[Introduction to Udemy Python3 + Application] 47. Process the dictionary with a for statement
(Matplotlib) I want to draw a graph with a size specified in pixels
Connect to postgreSQL from Python and use stored procedures in a loop.
If you want to count words in Python, it's convenient to use Counter.
Tips for Python beginners to use Scikit-image examples for themselves 3 Write to a file
Use a shortcut to enable or disable the touchpad in Linux Mint
[Pyto] I tried to use a smartphone as a flick keyboard for PC
How to run a quickstart for Google Cloud Text-to-Speech .NET in Unity
A memorandum because I stumbled on trying to use MeCab in Python
Add words to MeCab's user dictionary on Ubuntu for use in Python
I think it's a loss not to use profiler for performance tuning
It is convenient to use Layers when putting a library on Lambda
[Introduction to Python] How to use class in Python?
How to suppress display error in matplotlib
Use print in a Python2 lambda expression
Display matplotlib diagrams in a web application
Use a scikit-learn model trained in PySpark
How to use Google Test in C
How to get a stacktrace in python
Easy way to use Wikipedia in Python
Minimum knowledge to use Form in Flask
[Python] Organizing how to use for statements