“This is the 8th day of my participation in the Gwen Challenge.

1. Add text

Use the plt.title() function to add a title to the icon, and the plt.xlabel() and plt.ylabel() functions to add labels to the X and y axes, respectively, as follows:

Import matplotlib.pyplot as PLT x = [1,2,3] y = [4,5,6] plt.plot(x,y) plt.title(' title ') plt.xlabel(' x ') plt.ylabel(' y ') import matplotlib.pyplot as PLT x = [1,2,3] y = [4,5,6] plt.plot(x,y) plt.title(' title ') plt.xlabel(' x ') plt.ylabel(' y ')  plt.show()Copy the code

The running result is shown in the figure below.

2. Add a legend

Use the plt.legend() function to add a legend to the diagram. Before adding, set the label parameter as follows.

Import numpy as np import matplotlib.pyploy as PLT x1 = np.array([1,2,3]) y1 = x1 +1 plt.plot(x1,y1,label='y=x+1') y2=x1*2 plt.plot(x1,y2,color = 'red',linestyle='--',label='y=x*2') plt.legend(loc='upper left') plt.show()Copy the code

The result is shown below, where you can see two lines drawn and a legend added to the top left corner of the diagram. To change the position of the legend, you can change the value of the LOC parameter, such as ‘upper Right ‘for the upper right corner and ‘lower Right’ for the lower right corner.

3. Set two axes

As can be seen from the previous introduction, two lines can be drawn in a chart. However, if the value ranges of the two lines are very different, the effect of the drawn chart will not be beautiful. In this case, a double coordinate axis is needed to present the chart, that is, the Y-axis of the two lines is drawn separately. Add the following code after drawing the first line to set the double axes.

plt.twinx()
Copy the code

Note that if you have two axes, you have to add a legend every time you draw a diagram, not at the end. Here, take y = x and y = x2 as examples to demonstrate how to set the double axes. The code is as follows

Import numpy as np import matplotlib.pyplot as PLT x1=np.array([10,20,30]) y1=x1 plt.plot(x1,y1,color='red',linestyle='--',label='y=x') plt.legend(loc='upper left') plt.twinx() y2=x1*x1 plt.plot(x1,y2,label='y=x^2') plt.legend(loc='upper right') plt.show()Copy the code

The running result is shown in the figure below. It can be seen that the value ranges of the left and right y-axes are very different. If the double axes are not set, the y = x line will be compressed very flat, affecting the effect of data visualization.

4. Set chart size

If you are not satisfied with the default chart size, you can set the chart size with the following code.

PLT. RcParams [' figure. Figsize] = (8, 6)Copy the code

The first element represents length, the second represents width, and the numbers 8 and 6 represent 800 and 600 pixels respectively.

5. Set the Angle of the x scale

If the X-axis scale is too dense for easy reading due to the large scale content, you can adjust the scale Angle by setting the code as follows. Where 45 represents 45°, which can be changed to other values as needed.

plt.xticks(rotation = 45)
Copy the code

6. Solve Chinese display problem

When using the Matplotlib library to draw pictures, Chinese characters cannot be displayed normally by default. The following code can solve this problem. Since changing the font will not display the minus sign “-“, you need to set the axes. Unicode_minus parameter in the profile to False.

import matplotlob.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']= False
Copy the code

7. Draw the photos

Sometimes we need to draw multiple charts on one canvas, as shown below. This effect can be achieved using the Matplotlib concepts of current figure and current axes, which correspond to the current canvas and current axes. Multiple subgraphs can be drawn on one canvas. Plots are usually plotted by subplot() or subplots().