When you draw a coordinate chart using the Matplotlib module, you often need to set many parameters for the axis, including the horizontal axis range, the axis scale size, the axis name, and so on, which contains a number of functions in matplotlib to set these parameters. We can set the axis, set the axis range, set the text description on the axis, and so on. In this article, we will introduce you to the python matplotlib axis setup method.
Basic usage
For example:
Import NumPy as Npimport pandas as Pdimport Matplotlib.pyplot as plt# generates data on the x-axis: from 3 to 3, total 50 points x = Np.linspace (-1, 1, 50) # definition A linear equation y1 = 2 * x + # defines a two-time equation y2 = x * * * The value range of the x-axis is set to: 1 to 2plt.xlim (-1, 2) # Set the y-axis value range:-1 to 3plt.ylim (-1, 3) # Set the x-axis text to describe the x-axis generation What is the table Plt.xlabel ("I Am X") # Sets the y-axis of the text that describes what the y-axis represents Plt.ylabel ("I am y") plt.plot (x, y2) # Draws a line with a red line width of 1 dashed lines Plt.plot (x, y1, color = ' Red ', linewidth=1.0, linestyle= '--') # Show Chart plt.show ()
The output graph is:
Modify the scale of an axis
To modify the x-axis scale, from 1 to 2, a total of 5 points:
New_ticks = Np.linspace ( -1, 2, 5) plt.xticks (new_ticks)
As long as the above code is placed before the drawing, the final drawing is:
The scale on the x-axis is then modified to a total of 5 points from 1 to 2.
Show the scale in words
# Set Y-scale: Display ticks in text Plt.yticks ([-2, -1.8,-1, 1.22, 3], [' Really bad ', ' bad ', ' normal ', ' good ', ' really good '])
The above code means to map the values and text in the Y-scale:
-2 = ' Really bad '
-1.8 = ' bad '
-1 = ' normal '
1.22 = ' good '
3 = ' really good '
The graph shows:
Modify Axis Scale Font
In order to make the font on the scale look better, you can use:
Plt.yticks ([-2, -1.8,-1, 1.22, 3], [R ' $really \ bad$ ', R ' $bad \alpha$ ', ' normal ', R ' $good $ ', ' really good '])
The font is displayed in regular text, and \alpha is used to escape the alpha letters in the display, and the image is displayed as follows:
The above is the python matplotlib axis setting method, I hope to help everyone.
Related recommendations:
Summary of definitions and usage of axes
HTML5 Canvas coordinate conversion, hatch, gradient, and shadow
Python implements a way to draw a straight-line cursor between two axes in matplotlib