Changing Coordinate Axis Units Matplotlib
In this class, We discuss Changing Coordinate Axis Units Matplotlib.
The reader should have prior knowledge of line charts. Click here.
In our last class, we have discussed line charts.
The readability of the line chart is not proper. Because large units are taken in the y-axis.
We need to change the axis units. Below the code is a multiple-line plot from our previous class.
Changing Coordinate Axis Units
ylim method to change y-axis limits
The below program uses ylim method to change the limitation of the y-axis.
change y coordinate units using ylim
Multiple line plot code
import matplotlib.pyplot as plt
z=plt.figure(num=1,figsize=(10,5))
plt.plot(x, y1, color ='blue')
plt.plot(x,y2,color='red')
plt.plot(x,y3,color='green')
plt.ylim(-260000,260000)
plt.xticks(x)
plt.xlabel("Year")
plt.ylabel("Sum of sale")
plt.title("Year Wise Total Sale")
plt.legend(['Furniture','Office Supplies','Technology'],loc=0)
plt.show()
Change Units on Y-axis
We use the method yticks to change the unit measure on the y-axis.
In the same way, we can change the x-axis using xticks.
We used the arrange function to generate the y-axis units that are to be displayed.
The below code shows us how to change the y-axis unit measure.
changing Unit measure using yticks
import matplotlib.pyplot as plt
import numpy as np
z=plt.figure(num=1,figsize=(10,5))
plt.plot(x, y1, color ='blue')
plt.plot(x,y2,color='red')
plt.plot(x,y3,color='green')
plt.xticks(x)
plt.yticks(np.arange(130000,280000,10000))
plt.xlabel("Year")
plt.ylabel("Sum of sale")
plt.title("Year Wise Total Sale")
plt.legend(['Furniture','Office Supplies','Technology'],loc=0)
plt.show()
The output of the above code still does not have the clarity to check the data values in the line graph.
We can use grid style for better readability of graphs.
We can apply markers to have better readability of the graph.
Below is the code to give better clarity using grid style and markers.
We use the method grid to generate grid lines.
more clarity using markers and Grid
import matplotlib.pyplot as plt
import numpy as np
z=plt.figure(num=1,figsize=(10,5))
plt.plot(x, y1,'b-o')
plt.plot(x,y2,'r:s')
plt.plot(x,y3,'g--d')
plt.grid(True)
plt.xticks(x)
plt.yticks(np.arange(130000,280000,10000))
plt.xlabel("Year")
plt.ylabel("Sum of sale")
plt.title("Year Wise Total Sale")
plt.legend(['Furniture','Office Supplies','Technology'],loc=0)
plt.show()
Further clarity, we can apply major and minor ticks and grids. Discussed in our next classes.
Link for playlists:
/ @wisdomerscse
Link for our website: https://learningmonkey.in
Follow us on Facebook @ / learningmonkey
Follow us on Instagram @ / learningmonkey1
Follow us on Twitter @ / _learningmonkey
Mail us @ [email protected]
Информация по комментариям в разработке