[PYTHON] A quick reference chart of matplotlib needed to draw a diagram to convince your boss

When considering features such as machine learning, I want to draw various diagrams. Diagrams are also important for explaining to others (especially bosses, bosses, bosses ...). If you don't write it clearly, you will be forced to start over. I have listed the processes that may be useful at that time.

import

import matplotlib.pyplot as plt

Creating a diagram (fig, ax)

Single unit

import matplotlib.pyplot as plt
fig = plt.figure(figsize=(15, 8), dpi=100)
ax1 = plt.axes()

The fig size is (horizontal, vertical) inches, and the dpi is the number of pixels per inch. So in this case, it's a 1500 x 800 image. dpi = 100 is the default, figsize is the default (6,4), Recent displays are more horizontal and have higher image quality, so I set it to (15,8).

Multiple

fig = plt.figure(figsize=(15, 8), dpi=100)
ax1 = plt.subplot(2, 2, 1) #1st row, 1st column
ax2 = plt.subplot(2, 2, 2) #1st row, 2nd column
ax3 = plt.subplot(2, 2, 3) #2nd row, 1st column
ax4 = plt.subplot(2, 2, 4) #2nd row, 2nd column

Or collectively

fig, ax = plt.subplots(2, 2, figsize=(15, 8), dpi=100)
ax1 = ax[0, 0] #1st row, 1st column
ax2 = ax[0, 1] #1st row, 2nd column
ax3 = ax[1, 0] #2nd row, 1st column
ax4 = ax[1, 1] #2nd row, 2nd column

Alternatively, it can be added dynamically.

fig = plt.figure(figsize=(15, 8), dpi=100)
ax1 = fig.add_subplot(2, 2, 1)
ax2 = fig.add_subplot(2, 2, 2)
ax3 = fig.add_subplot(2, 2, 3)
ax4 = fig.add_subplot(2, 2, 4)

Drawing a diagram

Draw a line

import numpy as np
y = np.random.randn(100) #Sampling 100 pieces from the standard normal distribution
x = np.arange(y.shape[0])
ax1.plot(y, x, linewidth=0.5)

linewidth is the thickness of the line. I think I'll use color, linestype, marker, label, zorder, etc.

--linewidth ... Line thickness. --linestyle ... Specify practice and dotted lines. Click here for details (https://matplotlib.org/gallery/lines_bars_and_markers/line_styles_reference.html) --marker ... Marker specification. Click here for details (https://matplotlib.org/api/markers_api.html). --color ... The color of the line. Click here for details (https://matplotlib.org/api/colors_api.html) --label ... Used for legend. It can be displayed with ax.legend () described later. --zorder ... Specify the order of which to display on the front side and back side.

There are other kwargs that can use matplotlib.pyplot.plot, and the Line2D property can be used. For the time being here has a list.

Scatter plot

It is often used as a marker on ordinary scatter plots and waveforms.

ax.scatter(0.5, 0.5, s=600, c="red", alpha=0.1)

You can set s for size, c for color, and alpha for transparency.

letter

For example, use it when you want to represent a specific numerical value of the marker part. It's called the maximum value, but how much is it specifically? You can explain things like that without reading the scale.

ax.text(0.5, 0.5, "test", va="bottom", ha="left", color="red")

reference

A list of things you can draw can be found here (https://matplotlib.org/api/_as_gen/matplotlib.pyplot.html).

Arrange each ax

Give a title

ax1.set_title("title")

Limit the range of axes.

ax1.set_xlim([0.0, 1.0])
ax1.set_ylim([0.0, 1.0])

Control the spacing of the scales.

ax1.set_xticks([0.0, 0.5, 1.0])
ax1.set_yticks([0.0, 0.5, 1.0])

Actually, it is practical to create with np.linspace (minimum, maximum, number of ticks).

Label the set scale interval

ax1.set_yticklabels(["small", "During ~", "Big"])

Show legend

ax1.legend(["test"], fontsize=7, loc="upper left")

If the label property is specified during plot, the ["test"] part is unnecessary. You can control the font size with fontsize and the display location with loc. For the loc setting value, refer to the Location String in here.

Show axis label

ax1.set_ylabel("X axis")
ax1.set_ylabel("Y axis")

Show grid

ax.grid(b=True, linestyle=":")

Adjustment after displaying all ax

A spell that somehow prepares

plt.tight_layout()

Figure output

Displayed on the screen.

plt.show()

In an environment where there is nothing that can be displayed, the image is output as shown below.

plt.savefig("aaa.png ")

Close / delete figure

Close if showing.

plt.close()

Furthermore, the figure is discarded. If not destroyed, it may occupy memory.

plt.clf()

Recommended Posts

A quick reference chart of matplotlib needed to draw a diagram to convince your boss
How to draw a graph using Matplotlib
[Python] How to draw a histogram in Matplotlib
[Python] How to draw a scatter plot with Matplotlib
I tried to draw a configuration diagram using Diagrams
Instantly create a diagram of 2D data using python's matplotlib