[PYTHON] How to draw a graph using Matplotlib

Overview

Summarize how to draw a graph in Matplotlib as a memorandum for yourself.

When you want to draw one graph for the time being

Just set the horizontal axis (x) and the vertical axis (y) and make it plt.plot (x, y).

Draw one for the time being.py


import matplotlib.pyplot as plt
import numpy

x = np.linspace(0, 10)
y = np.sin(x)
plt.plot(x, y)
plt.show()

matplotlib_base.png

plt arguments

c: Color. You can change the color of the graph with "r", "g", "b", "c", "m", "y", etc. marker: Specify the shape of the marker. There are "o", "^", "v", "+" and so on. lw: Specify the line thickness. Set to 0 when you want to erase the line. alpha: Specify transparency. The closer it is to 0, the closer it is to transparency. label: Specify a name. When drawing multiple graphs, give them a name. (Can be displayed in the legend)

plt arguments.py


x = np.linspace(0, 10)
y = np.sin(x)
plt.plot(x, y, c="r", marker="^", lw=1, alpha=0.5, label="test")
plt.show()

matplotlib_hikisu.png

Added various information to the graph

You can add various information to the graph with plt. 〇〇. Add title with plt.title Add x-axis / y-axis labels in plt.xlabe / ylabel Specify the minimum and maximum values of x-axis / y-axis with plt.xlim / ylim

Add information to the graph.py


x = np.linspace(0, 10)
y = np.sin(x)

plt.plot(x, y, c="c", marker="+", lw=0, alpha=0.5, label="test1")
plt.title("Figure_test")
plt.xlabel("axis_x")
plt.ylabel("axis_y")
plt.xlim(0, 15)
plt.ylim(np.min(y)-2, np.max(y)+2)
plt.grid()
plt.legend()

plt.show()

matplotlib_3.png

Draw multiple graphs

First, prepare a large box with plt.figure (). The size of the box can be specified by figsize. An image of putting multiple graphs in the prepared box with plt.subplot (). Specify the row in the first argument of plt.subplot (), the column in the second argument, and the number in the third argument. The numbers start from 1 in the upper left and increase in the order of right and bottom.

Display multiple graphs.py


x = np.linspace(0, 10)
y1 = np.sin(x)
y2 = x
y3 = x ** 4
y4 = np.cos(2*x)

plt.figure(figsize=(8, 5))

plt.subplot(2, 2, 1) #upper left
plt.plot(x, y1, c="r", marker="^", lw=1, alpha=0.5, label="left_up")

plt.subplot(2, 2, 2) #Upper right
plt.plot(x, y2, c="g", marker="o", lw=1, alpha=0.5, label="right_up")

plt.subplot(2, 2, 3) #Lower left
plt.plot(x, y3, c="b", marker="v", lw=1, alpha=0.5, label="left_down")

plt.subplot(2, 2, 4) #Lower right
plt.plot(x, y4, c="y", marker="+", lw=1, alpha=0.5, label="right_down")

plt.show()

matplotlib_4.png

It can also be written as follows. (The result is the same as ↑) In subplots (), first specify where to place the graph in a large box called fig. Draw a graph by specifying the location with ax [row, column]. (I think I'll summarize this writing style separately.)

Show multiple graphs 2.py


x = np.linspace(0, 10)
y1 = np.sin(x)
y2 = x
y3 = x ** 4
y4 = np.cos(2*x)

fig, ax = plt.subplots(2, 2, figsize=(8, 5))

#upper left
ax[0, 0].plot(x, y1, c="r", marker="^", lw=1, alpha=0.5, label="left_up")

#Upper right
ax[0, 1].plot(x, y2, c="g", marker="^", lw=1, alpha=0.5, label="right_up")

#Lower left
ax[1, 0].plot(x, y3, c="b", marker="^", lw=1, alpha=0.5, label="left_down")

#Lower right
ax[1, 1].plot(x, y4, c="y", marker="^", lw=1, alpha=0.5, label="right_down")

plt.show()

matplotlib_5.png

Summary

It's all very basic, but I wrote only the frequently used functions for myself as a beginner. If you have any recommended features, please comment.

Recommended Posts

How to draw a graph using Matplotlib
[Python] How to draw a line graph with Matplotlib
How to draw a 2-axis graph with pyplot
How to draw a 3D graph before optimization
[Python] How to draw a histogram in Matplotlib
How to draw a bar graph that summarizes multiple series with matplotlib
[Python] How to draw a scatter plot with Matplotlib
Draw a loose graph with matplotlib
How to install a package using a repository
Graph drawing using matplotlib
How to use Matplotlib
Draw a flat surface with a matplotlib 3d graph
How to code a drone using image recognition
[Python] How to draw multiple graphs with Matplotlib
Sample to draw a simple clock using ebiten
Draw a graph with matplotlib from a csv file
How to upload to a shared drive using pydrive
How to uninstall a module installed using setup.py
(Matplotlib) I want to draw a graph with a size specified in pixels
How to write a GUI using the maya command
Draw a graph with NetworkX
How to set up a Python environment using pyenv
How to call a function
How to hold a hands-on seminar using Jupyter using docker
How to hack a terminal
How to make a Python package using VS Code
[Python] How to create a 2D histogram with Matplotlib
How to execute a command using subprocess in Python
Draw a graph with networkx
I tried to draw a configuration diagram using Diagrams
How to avoid the cut-off label of the graph created by the plot module using matplotlib
Study math with Python: Draw a sympy (scipy) graph with matplotlib
How to transpose a 2D array using only python [Note]
How to add new data (lines and plots) using matplotlib
How to generate a query using the IN operator in Django
[Visualization] I want to draw a beautiful graph with Plotly
How to install python using anaconda
How to make a Japanese-English translation
How to write a Python class
Draw a graph with Julia + PyQtGraph (2)
How to put a symbolic link
Try to draw a Bezier curve
How to make a slack bot
How to create a Conda package
How to make a crawler --Advanced
How to make a recursive function
How to create a virtual bridge
Draw a graph with Julia + PyQtGraph (1)
How to run matplotlib on heroku
Draw a graph with Julia + PyQtGraph (3)
Draw retention rate graph in Matplotlib
How to make a deadman's switch
How to create a Dockerfile (basic)
[Blender] How to make a Blender plugin
Draw a graph with pandas + XlsxWriter
How to delete a Docker container
How to make a crawler --Basic
Easy to draw graphs with matplotlib
Draw a graph with PySimple GUI
How to create a config file
How to easily draw the structure of a neural network on Google Colaboratory using "convnet-drawer"