Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to set axis label and spacing by matplotlib

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how matplotlib sets axis labels and spacing. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Xlim, ylim Let's first introduce the range of axes, the range of axes is easy to understand, and sometimes the range of data we produce may not be exactly what we want. If we do not set the range of the axis, then matplotlib will automatically choose the most appropriate range according to the range of our data to display all the data.

To take a very simple example, suppose we want to draw a parabola in the range of-10 to 10. I think you are already very familiar with the method of drawing this image:

Since the range of x is-10 to 10, then the corresponding range of y should be [0100]. None of this should be difficult to understand, but what if we only want to show the range of [- 3,3] although the data is very comprehensive in the range of [- 10,10]? At this point we can use the xlim function to set the range of the x-axis, but it is important to note that when we call xlim, we only limit the result of the x-axis, not the y-axis. So draw the range of the y-axis or the range of the previous data, so it will look a little strange, the image will look like this:

The solution is also very simple, that is, set the scope of ylim, and set the range of ylim just enough to display the data appropriately. For example, in this example, it is appropriate to set ylim to [0,30] or [- 5,25].

There are some other parameters officially provided by xlim and ylim. If you are interested, you can consult the relevant documentation. It is suggested that there is no need to waste this time, because it is really not needed. Even xlim and ylim are rarely used.

Xticks and yticks if the previous xlim and ylim seem to be of some use, but do not know when they will be available, then the xtick and ytick introduced in this paragraph are very useful and should not be missed. This is not my brag, because compared with these two functions, xlim and ylim are really just younger brothers.

Basically, what xlim can set is the range of axes, while xticks and yticks can set both the range and the spacing between each scale. Also use the parabola example just now, in the example just now, our x-axis range is [- 10,10]. In the default image, the spacing that the system chooses for us is 2.5, that is, every 2.5 draws a coordinate point, a total of 8.

Through xticks, we can set the interval and number of coordinate points ourselves, for example, suppose we want to draw a coordinate point every five intervals on the x-axis, we can set it like this:

We put a list directly into the xticks, and finally the x-axis of the image we draw is divided according to this list. In other words, we can define not only the range of the axis, but also its interval. Sometimes, the range and spacing of the automatically drawn image may not be so good, and we need to adjust it, so we need to use the xticks and yticks functions.

In addition to setting the interval and range, xticks can also set the label and the rotation angle of the label. Let's also look at an example, in which we will convert the x-axis numbers in the above picture into English words and rotate them by 30 degrees.

One of the charts we often see in our lives is a company's performance sales table, or a growth curve table. One feature of this kind of table is that its x-axis is month, so that we can clearly see the monthly sales data, and can anticipate and guide the future. Before you learn how to use the xticks function, you may find this very difficult, but with xticks, it is actually very simple:

Thank you for reading! This is the end of this article on "how to set axis labels and spacing in matplotlib". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report