In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the meaning of plt and ax in Matplotlib. It has a certain reference value. Interested friends can refer to it. I hope you can learn a lot after reading this article. Let's take a look at it.
Concept
Note that when I say "plt", it does not exist in the Matplotlib library. It is called "plt" because most Python programmers like to import Matplotlib and create an alias called "plt", which I'm sure you should know.
Import matplotlib.pyplot as plt
Then, let's get back to our subject. To demonstrate, let's draw a simple chart.
Import numpy as npplt.plot (np.random.rand (20)) plt.title ('test title') plt.show ()
As shown in the screenshot above, when we use plt:
A figure object will be generated (shown in green)
Axes objects are implicitly generated by drawing a line chart (shown in red)
All elements in the figure, such as the x and y axes, are rendered in the Axes object (shown in blue)
This means that:
Figure is like a piece of paper. You can draw anything you want.
We have to draw a chart in the "cell", where Axes is the axis
If we draw only one figure, we don't need to draw a "cell" first, just draw it on paper. We can use plt.plot (...)
Explicitly draw "cells"
Of course, we can explicitly draw a "cell" on the "paper" to tell Matplotlib that we will draw a chart in this cell. Then, we have the following code.
Fig, ax = plt.subplots () ax.plot (np.random.rand (20)) ax.set_title ('test title') plt.show ()
The result is exactly the same. The only difference is that we explicitly draw the "cell" so that we can get the Figure and Axes objects.
In fact, when we only want to draw a graph, there is no need to "draw" the cell. However, you must note that we have to do this when we want to draw more than one graph in one diagram.
N_rows = 2 n_cols = 2fig, axes = plt.subplots (n_rows, n_cols) for row_num in range (n_rows): for col_num in range (n_cols): ax = axes [row _ num] [col_num] ax.plot (np.random.rand (20)) ax.set_title (f'Plot ({row_num+1}) {col_num+1})') fig.suptitle ('Main title') fig.tight_layout () plt.show ()
In this code snippet, we first declare the number of rows and columns to "draw". 2 × 2 means we have to draw four cells.
Then, in each cell, we draw a random line chart and assign a title according to its line number and column number. Note that we are using an axis instance.
After that, we define a "Main title", or Figure instance, on "paper". So, we have this super title, it doesn't belong to any "cell", but on the screen.
Finally, before calling the show () method, we need to ask the Figure instance to automatically provide enough padding between the cells by calling its tight_layout () method.
Thank you for reading this article carefully. I hope the article "what is the meaning of plt and ax in Matplotlib" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.