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 matplotlib.pyplot as plt
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).
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)
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.
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.
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")
A list of things you can draw can be found here (https://matplotlib.org/api/_as_gen/matplotlib.pyplot.html).
ax1.set_title("title")
ax1.set_xlim([0.0, 1.0])
ax1.set_ylim([0.0, 1.0])
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).
ax1.set_yticklabels(["small", "During ~", "Big"])
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.
ax1.set_ylabel("X axis")
ax1.set_ylabel("Y axis")
ax.grid(b=True, linestyle=":")
plt.tight_layout()
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 if showing.
plt.close()
Furthermore, the figure is discarded. If not destroyed, it may occupy memory.
plt.clf()
Recommended Posts