Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

Add secondary mesh

When we look at a graph, we may need to quickly estimate the coordinates of a part of the graph, and adding an auxiliary grid to the graph is a good way to improve the readability of the graph.

import numpy as np
import matplotlib.pyplot as plt
data = np.random.standard_normal((150.2))
plt.scatter(data[:,0],data[:,1],c='y')
plt.grid(True, lw=. 5, ls=The '-', c='c')
plt.show()
Copy the code

Grid () is made up of lines, so plt.grid() can accept linestyle arguments (such as linewidth, linestyle, color, etc.) to modify lines in a secondary network.

Add auxiliary wire

Since all the graphs generated by Matplotlib consist of basic primitives, we can use a basic primitive: lines to add auxiliary lines.

import matplotlib.pyplot as plt
N = 16
for i in range(N):
    plt.gca().add_line(plt.Line2D((0, i), (N - i, 0), color = 'm'))
plt.axis('tight')
plt.show()
Copy the code

The plt.line2d () function does the following: Create a new Line2D object. The necessary parameters are the two endpoints of the line segment. The optional parameters include all the linestyle parameters described above (linewidth, linestyle, color, etc.). The plt.gca() function returns the object responsible for tracking the rendered content. Call plt.gca().add_line() to render the created Line2Dd object. You can use the plt.axis() function to change the scale of the graph. If this function is not called, the graph will not be fully displayed.

Series of links

Matplotlib common statistical graph drawing

Matplotlib uses custom colors to draw statistics

Matplotlib controls line style and line width

Matplotlib custom style to draw beautiful statistics

Matplotlib adds text instructions to the graph

Matplotlib adds comments to the graph