In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how Matplotlib adjusts the legend. Xiaobian thinks it is quite practical, so share it with you for reference. I hope you can gain something after reading this article.
Legend can be set in Matplotlib using plt.legend() and ax.legend() in essence, both use the same method and receive parameters, this article takes plt.legend() as an example to explain those things set in Matplotlib legend.
As with previous component settings articles, this article will not list too many boring parameters, only use multiple cases to introduce, which settings you like, take away directly.
The first is the simplest legend setting. In matplotlib, set the legend using plt.legend(), provided that you add label to the corresponding data in the process of drawing (of course, you can manually set it later, if you don't like it, it is not recommended to use it, so I don't write it here)
plt.figure(figsize =(7,6),dpi=100)
plt.title(Legend Settings)
plt.plot([1,2,3,4,5],[2,3,4,5,6],label = 'I am Legend')
plt.legend()
plt.show()
If you are not satisfied with the font size of the legend, you can change the fontsize parameter to adjust it. In fact, any font-related method in Matplotlib can be adjusted using fontsize.
plt.legend(fontsize = 12)
In legend settings, besides font size, the other thing that needs to be adjusted most is the legend position. The legend generated by default is automatically selected by the system. However, in many cases, we want to adjust the legend position. In this case, we can use loc parameter. When it comes to loc, look at the following figure.
loc parameter is not much to say, want to let the legend in which position, according to the figure to choose a position to type the corresponding code into the line, you can use the above string can also use the following numbers, two equivalent!
So how to put the legend outside the border? At this time, you can change the bbox_to_anchor parameter. What is this bbox_to_anchor? It accepts a coordinate position to adjust the legend position relatively.
Simply put, after you use loc = xxxx, you can use this relative position to move the legend, see an example to feel it
plt.legend(fontsize = 12,loc = 1,bbox_to_anchor=(1.3,1))
We use bbox_to_anchor=(1.3,1) to move the legend to the right of the figure. As for how to determine the two values in this, I can't say clearly. The best way is to "set a group randomly, and then modify it yourself. Keep adjusting until the position is satisfactory."
Now, the legend settings in the most concerned about the two points (size, position) to get done, the rest is a number of decorative settings, a brief talk about ah.
Use edgecolor to modify the legend border color
plt.legend(fontsize = 13,edgecolor='red')
Using the frameon parameter, you can choose whether to keep the legend border
plt.legend(fontsize = 13,frameon=False)
facecolor allows you to add fill color to a legend
plt.legend(fontsize = 13,edgecolor= 'blue',facecolor='pink')
About "Matplotlib how to adjust the legend" this article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please 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.
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.