Matplotlib --Graph drawing library -Official site --Use in combination with basic Python + Numpy + matplotlib + SciPy + etc.
pip install matplotlib
import matplotlib.pyplot as plt #curse
squares = [1, 4, 9, 16, 25]
plt.plot(squares) #In default, points are connected by a straight line
plt.show() #Follow the instructions above to draw and show
The default is that Japanese cannot be used, so a little setting is required. In short, change the font setting of ** matplotlib ** to the font that supports Japanese.
font.family: IPAexGothic
under # font.family: sans-serif
You can find out the font path and cache location with the code below
print(matplotlib.matplotlib_fname()) #Font path
print(matplotlib.get_cachedir()) #Where to save the cache
The other way is not to modify the config file, but you have to add the following code every time
plt.rcParams['font.sans-serif']=['IPAexGothic']
Or
plt.rcParams["font.family"] = "IPAexGothic"
import matplotlib.pyplot as plt
squares = [1, 4, 9, 16, 25]
plt.plot(squares, linewidth = 5) #Make the line a little thicker
plt.title("square", fontsize = 24)
plt.xlabel("value", fontsize = 14)
plt.ylabel("The square of the value", fontsize = 14)
#Axis scale display settings
plt.tick_params(axis = 'both', labelsize = 14)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
#0 to 5 to 0.Lines look smooth by creating 2-step dots
x = np.arange(0., 5., 0.2)
plt.plot(x, x * x)
plt.title("square", fontsize = 24)
plt.xlabel("value", fontsize = 14)
plt.ylabel("The square of the value", fontsize = 14)
#Set the font size of the scale to a slightly larger size
plt.tick_params(labelsize = 30)
plt.show()
If you increase the size of the scale, the X-axis and Y-axis labels will stick out and you will not be able to see them. Of course, you can adjust the screen size to make them visible, but you can change plt.tight_layout ()
to plt. If you put it before show ()
, it will be displayed automatically.
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0., 5., 0.2)
plt.plot(x, x * x)
plt.title("square", fontsize = 24)
plt.xlabel("value", fontsize = 14)
plt.ylabel("The square of the value", fontsize = 14)
plt.tick_params(labelsize = 30)
plt.tight_layout()
plt.show()
Use scatter ().
import matplotlib.pyplot as plt
x_values = [1, 2, 3, 4, 5]
y_values = [1, 4, 9, 16, 25]
plt.scatter(x_values, y_values, s=100) #s specifies the size of the point
plt.show()
Data is obtained from automatic calculation
import matplotlib.pyplot as plt
x_values = list(range(1, 1001))
y_values = [x**2 for x in x_values]
plt.scatter(x_values, y_values, s=4)
plt.axis([0, 1100, 0, 1100000]) #Axis range
plt.show()
You can specify the color with the parameter c
plt.scatter(x_values, y_values, c='red', s=40)
It can also be specified in RGB. It is also possible to specify each RGB color element with a tuple of 0.0 to 1.0.
plt.scatter(x_values, y_values, c=(0, 0, 0.8), s=40)
A color map represents the correspondence between values and colors used when drawing. When visualizing data, the selection of color maps can emphasize the rules of data change. For example, a light color represents a small value, and a dark color emphasizes a large value.
import matplotlib.pyplot as plt
x_values = list(range(1, 1001))
y_values = [x**2 for x in x_values]
plt.scatter(x_values, y_values, c=y_values, cmap=plt.cm.Blues)
plt.show()
For details, refer to Official Site. Examples ⇒ Color
Use plt.savefig ()
instead ofplt.show ()
plt.savefig(`squares_plot.png`, bbox_inches='tight')
If you specify bbox_inches ='tight'
, extra whitespace will be cut off.
The extension of the file that can be saved is
eps, jpeg, jpg, pdf, pgf, png, ps, raw, rgba, svg, svgz, tif, tiff
plt.axes().get_xaxis().set_visible(False)
plt.axes().get_yaxis().set_visible(False)
plt.figure(figsize=(10, 6)) #The unit is inches
plt.figure(dpi=128, figsize=(10, 6)) #The default is 80dpi
Recommended Posts