![]() |
Why areMatplotlib is a powerful Python library, with the help of which we can draw bar graphs, charts, plots, scales, and more. In this article, we’ll try to draw multiple Y-axis scales in Matplotlib. Why are multiple Y-axis scales important?Multiple Y-axis scales are necessary when plotting datasets with different units or measurement scales, aiding in clear comparison without distortion. This is necessary when:
Importing necessary articlesimport matplotlib.pyplot as plt Here we also need to import NumPy, as we’ll be needing that later. Creating Sample dataHere, we will be using NumPy for this part. Python3
We’re going to create our first Y-axis. To create an axis, we use Matplotlib. 1. Creating the First Y-Axislet’s create our first Y-Axis: Python3
Output: Here, we’ve created a matplotlib figure and axis “ax1” to represent the first y-axis. “plt.subplots(” basically used to create single subplot and also figures. 2. Plotting the First Dataset on the First Y-AxisNow our next step is to plot the first data set on the first Y-Axis. Here, we plot the first dataset that is “y1” we defined and plot on the first Y-Axis “ax1” with the help of “ax1.plot()”. Here we used “b” as the color, as we want it to be blue and then we set the lables of X and Y axes. Also we’ve used “tick_params()” to set the color of Y-Axis to blue. Python3
Output: Our first Y-Axis is successfully created, now we’ll create a second Y-Axis that will share the same X-Axis. We use trinx() function to get the desired result. 3. Creating a Second Y-AxisPython3
Here, we’re creating a second axis “ax2″ that will share the same X-Axis with the first Y-Axis using “ax1.twin()” method. 4. Plotting the Second Dataset on the Second Y-AxisHere, we are plotting a second dataset on the second y-axis we’ve created above. Now we’re using “g” as we want to set the color to green. Then it sets the labels for the y-axis. By using “tick_params()” we can set the color of Y-Axis to blue. Now we will plot the second data set on the second Y-Axis. Python3
Output: Let’s then create a third Y-Axis that will share the same X-Axis that shared before. 5. Creating a Third Y-AxisPython3
Our next step is to plot the third data set on the third Y-Axis. 6. Plotting the Third Dataset on the Third Y-AxisHere, we are plotting a second dataset on the second y-axis we’ve created above. Now we’re using “g” as we want to set the color to green. Then it sets the labels for the y-axis. By using “tick_params()” we can set the color of Y-Axis to blue. Python3
Output: 7. Adding LegendsHere comes our legendary part. We’ll now add the legends by using the “legend()” and get_legend_handles_lablels()” functions. Python3
8. Title and Displaying the PlotAnd we’re all done. Now our last step is to title and then show the plot that will create the multiple Y-Axis Scales and show us the plot, amalgamating all the plots above. Python3
And we’re done. We’ve discussed from starting to the end on how to create and show mutiple Y-Axis Scales with the help of matplotlib. Let’s now see how our whole project looks like. Finally,. we title and display out plot using “plt.title()” and “plt.show()”. It’ll show us our complete plot with the multiple y-axis scales as we have seen in the result above. In short this creates a single plot with the three y-axes with a single x-axis. We’ve made it easy to visualize and compare multiple datasets with different scales on a single plot. Python3
Output: In this image, we can clearly see the three y-axes and x-axis and understand how well our plot has been drawn. ConclusionWe’ve discussed on how to import the required libraries i.e. matplotlib and numpy in this project, then we created sample data for multiple data sets, we then discussed about first y-axis, second y-axis, third y-axis and in between we took help of the “twinx()” function to create the y-axes that shares the same x-axis. Then after adding legends we finally plotted our scales and successfully drawn the scales. Hope this was helpful in craeting multiple Y-Axis Scales in Matplotlib. |
Reffered: https://www.geeksforgeeks.org
AI ML DS |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |