Summarize how to draw a graph in Matplotlib as a memorandum for yourself.
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()
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()
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()
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()
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()
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