Matplotlib
#It has a lot of functions for visualizing data.
matplotlib.pyplot.plot(Data corresponding to the x-axis,Data corresponding to the y-axis)
#You can easily create a graph by associating data with the x-axis (horizontal axis) and y-axis (vertical axis) of the graph.
matplotlib.pyplot.show() #Display the created graph on the screen
When creating a graph using matplotlib.pyplot The display range of the graph is set automatically.
The min () and max () of the data (list) assigned to each axis are All parts of the data are automatically visualized because the display range is the minimum and maximum values.
You may want to display only part of the graph. In that case Use the program below.
matplotlib.pyplot.xlim([Starting value,End value])
#Set the display range of the graph
#xlim is a function that specifies the range of the x-axis. Use ylim to specify the y-axis range
matplotlib.pyplot has many methods that allow you to name various elements of your graph.
matplotlib.pyplot.title("title") # グラフのtitle
matplotlib.pyplot.xlabel("x-axis label") # グラフのx-axis label
The grid (scale line) is the display method.
matplotlib.pyplot.grid(True) #Display of grid(None by default)
When you create a graph, the x-axis and y-axis are automatically graduated. The sharpness of each axis is good and the scale is attached. If you want to set the scale, use the following program.
matplotlib.pyplot.xticks(Position to insert the scale,Character string of the scale to be displayed)
#Set scale on x-axis
import matplotlib.pyplot as plt
plt.plot([2,3,9,1,5])
plt.xticks([0.7,2.1,3.5], ["i","ii","iii"])
plt.show()
Sometimes you want to display multiple data on one graph. Each data can be displayed (plot) in different colors on the graph.
matplotlib.pyplot.plot(x, y, color="Color specification")
#Describe each data you want to display in different colors, and specify different variables for each.
#Each time you do this, a plot will be drawn on the graph.
# color=Even without it, you can specify the color with the third argument.
The color of the plot is specified by the HTML color code.
It is a code that expresses the color with 6-digit hexadecimal numbers (0-9 and A-F alphanumeric characters) following #, such as 0000ff. AA0000 is a color close to red.
You can also specify the following characters.
Color code | color |
---|---|
b | Blue |
g | Green |
r | Red |
c | cyan |
m | Magenta |
y | yellow |
k | black |
w | White |
matplotlib.pyplot.legend() #Set and display the series label (legend).
It is the lower left part of the figure below.
There are two ways to set the series label.
# 1.Automatically determine what is displayed on the series label
matplotlib.pyplot.plot(x, y1, label="Label name 1")
matplotlib.pyplot.plot(x, y2, label="Label name 2")
matplotlib.pyplot.legend()
# 2.Label elements that already exist
matplotlib.pyplot.plot(x, y1)
matplotlib.pyplot.plot(x, y2)
matplotlib.pyplot.legend(["Label name 1", "Label name 2"])
Method 1 explicitly specifies the label for the element to be plotted. In method 2, the relationship between the element and the label is not explicit. Not recommended as it can cause confusion.
How to create multiple graphs and edit them
matplotlib.pyplot.figure()
#Methods that can operate everything in the figure
matplotlib.pyplot.figure(figsize=(Horizontal size,Vertical size))
#Size with figsiza(In inches)Specify.
#Specify the horizontal and vertical sizes in inches. If omitted, figsize=(8, 6)Will be.
Using the subplots (axes), in the figure (figure) You can generate any number of graphs and draw multiple graphs. You can also manipulate the graph for each subplot.
When adding a subplot (axes object) to a diagram (figure object) Specify the add_subplot () method for the figure object Specifies the layout that divides the figure and the position of the subplot within it.
add_subplot(Number of lines,Number of columns,What number)
#Number of lines: How many lines should the figure be divided into?
#Number of columns: How many columns will the figure be divided into?
#Number: 1 from top left to right in the figure, 2, 3 ...What number do you add?
import matplotlib.pyplot as plt
import numpy as np
#Create figure object
fig = plt.figure(figsize=(4, 4))
#Divide the axes object into 2 rows and 3 columns and add a graph at the bottom right
ax = fig.add_subplot(2, 3, 6)
# y=Draw x graph on axes object
x = np.linspace(0, 100)
y = x
ax.plot(x, y)
#Fill in the blanks with subplots to make the position of the graph easier to understand
for i in range(6):
if i == 5:
continue
fig.add_subplot(2, 3, i+1)
plt.show()
matplotlib.pyplot.subplots_adjust(wspace=Horizontal spacing width, hspace=Vertical spacing width)
#There is no limit to the value that sets the margin, but it is automatically corrected so that it does not jump out of the graph area.
You can set the graph display range for each subplot. In addition, the x-axis and y-axis can be set respectively.
#Let the subplot object be the variable ax.
ax.set_xlim([minimum value,Maximum value]) #Set the x-axis display range
ax.set_ylim([minimum value,Maximum value]) #Set the y-axis display range
#For example, to set the x-axis display range to 0 to 1, write as follows.
ax.set_xlim([0, 1])
You can set elements such as titles and labels for each subplot. There is a little habit in the way.
#Let the subplot object be the variable ax.
ax.set_title("title") # グラフのtitleを設定する
ax.set_xlabel("x-axis name") #Set x-axis label
ax.set_ylabel("y-axis name") #Set the y-axis label
#Let the subplot object be the variable ax.
ax.grid(True)
You can set the axis scale for each subplot.
#Let the subplot object be the variable ax.
ax.set_xticks([List of insertion positions]) #Position of the scale to be inserted on the x-axis
ax.set_xticklabels([List of tick labels]) #Tick label to insert on x-axis
#Describe the position of the scale to be inserted and the label of the scale in a list type.
#It is also possible to convert it to a list type variable in advance.
Recommended Posts