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.

Use logarithmic scale

When the range of the visualized data is very wide, if the conventional coordinate scale is still used, the data will be displayed intensively, and even the change trend of the data cannot be seen. In this case, the logarithmic scale can be used to better display the graph.

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(1.10.1024)
plt.yscale('log')
plt.plot(x, x, c = 'c', lw = 2., label = r'$f(x)=x$')
plt.plot(x, 10 ** x, c = 'y', ls = The '-', lw = 2., label = r'$f(x)=e^x$')
plt.plot(x, np.log(x), c = 'm', lw = 2., label = r'$f(x)=\log(x)$')
plt.legend()
plt.show()
Copy the code

If you use the regular axis scale, the graph will become confused:

Tips: Get the logarithmic scale by passing the ‘log’ parameter to the plt.yscale() function; Other available scaling type parameter values include ‘Linear’, ‘symlog’, etc. Similarly, we can use plt.xscale() to get the same result on the X-axis. By default, the logarithmic base is 10, but can be changed with optional parameters basex and basey. Setting the logarithmic scale applies to any graph, not just a graph. Similarly, using the logarithmic scale can be used to magnify a small range over a very large range of data:

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-100.100.4096)
plt.xscale('symlog', linthreshx=6.)
plt.plot(x, np.sinc(x), c = 'c')
plt.show()
Copy the code

Tips: Using "symlog" as the parameter value of plt.xscale(), you can set a symmetric logarithmic scale centered on 0. For example, by setting "Linthreshx =6", the range of the logarithmic scale is [-6, 6], at which point the logarithmic scale is used within the range [-6, 6], and beyond that the linear scale is used. In this way, we can look at data in a range in detail while still seeing the general characteristics of a large number of data outside the range.

Using polar coordinates

Some graphics have a close relationship with angles. For example, the power of a speaker depends on the Angle of measurement. At this point, polar coordinates are the best choice for representing such data relationships.

import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(0 , 2 * np.pi, 1024)
plt.axes(polar = True)
plt.plot(t, 1. + 25. * np.sin(16 * t), c= 'm')
plt.show()
Copy the code

Tips: PLT.axes () allows you to explicitly create a AXES instance for custom Settings. Simply use the optional polar parameter to set up polar coordinates.

Although drawing curves is probably the most common use of polar coordinates. However, we can also use polar coordinates to draw any other type of graph, such as bar charts and shapes. For example, using polar coordinates and polygons, a radar diagram can be drawn:

import numpy as np
import matplotlib.patches as patches
import matplotlib.pyplot as plt
ax = plt.axes(polar = True)
theta = np.linspace(0.2 * np.pi, 8, endpoint = False)
radius = 25. + 75. * np.random.random(size = len(theta))
points = np.vstack((theta, radius)).transpose()
plt.gca().add_patch(patches.Polygon(points, color = 'c'))
plt.show()
Copy the code

Tips: The polygon coordinates used here are the angles and distances between the vertices of the polygon and the origin, without the need to perform an explicit conversion from polar to Cartesian coordinates.

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

Matplotlib adds auxiliary grids and auxiliary lines to the graph

Matplotlib adds custom shapes

Matplotlib controls the scale spacing and labeling of the coordinate axes