SoFunction
Updated on 2024-10-28

Scale intervals and scale ranges for axes set by Python.

I. Plotting line graphs with default settings

import  as plt
 
x_values=list(range(11))
# The numbers on the x-axis are the 11 integers from 0 to 10.
y_values=[x**2 for x in x_values]
#The number on the y-axis is the square of the number on the x-axis.
(x_values,y_values,c='green')
# Plot a line graph using the plot function, with the line color set to green
('Squares',fontsize=24)
# Set the chart title and title font size
plt.tick_params(axis='both',which='major',labelsize=14)
# Set the font size of the scale
('Numbers',fontsize=14)
# Set the x-axis labels and their font size
('Squares',fontsize=14)
# Set the y-axis label and its font size
()
#Show Chart

This produces the chart shown below:

We want the x-axis scale to be 0,1,2,3,4 ...... and the y-axis scale to be 0,10,20,30 ...... and would like the range of both axes to be a little bit larger, so we need to set it manually.

Second, manually set the coordinate axis scale interval and scale range

import  as plt
from  import MultipleLocator
# Import the MultipleLocator class from pyplot, this class is used to set the scale intervals
 
x_values=list(range(11))
y_values=[x**2 for x in x_values]
(x_values,y_values,c='green')
('Squares',fontsize=24)
plt.tick_params(axis='both',which='major',labelsize=14)
('Numbers',fontsize=14)
('Squares',fontsize=14)
x_major_locator=MultipleLocator(1)
# Set the x-axis scale interval to 1 in the variable
y_major_locator=MultipleLocator(10)
# Set the y-axis scale interval to 10 and put it in the variable
ax=()
Example of #ax for two axes
.set_major_locator(x_major_locator)
# Set the x-axis primary scale to a multiple of 1
.set_major_locator(y_major_locator)
# Set the main y-axis scale to a multiple of 10
(-0.5,11)
# Set the x-axis scale range to -0.5 to 11, because 0.5 is not satisfied with a scale interval, so the number will not show up, but you can see a little blankness
(-5,110)
# Set the y-axis scale range to -5 to 110, ditto, -5 won't be labeled but you can see a bit of blankness
()

The result of the plotting is shown in Fig:

This is the whole content of this article.