In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "what are the skills for the use of Seaborn". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Base diagram
The script in this article was tested in python3.8.3 in the Jupyter notebook.
Let's use Seaborn's built-in penguins dataset as sample data:
# Import package import matplotlib.pyplot as plt import seaborn as sns # Import data df = sns.load_dataset ('penguins') .rename (columns= {' sex': 'gender'}) df
We will use the default chart settings to build a standard scatter chart to use as a baseline:
# figure sns.scatterplot (data=df, alpha=0.7, hue='species', size='gender')
We will see how this diagram changes with each technique.
Sports Acrobatics
As you will see, the first two techniques are used for a single drawing, while the remaining four techniques are used to change the default settings for all charts.
Tip 1: semicolon
Have you noticed that in the previous picture, the text output is directly at the top of the chart? A simple way to suppress this text output is to use; at the end of the drawing.
# figure sns.scatterplot (data=df, alpha=0.7, hue='species', size='gender')
Just add at the end of the code; you can get clearer output.
Skill 2:plt.figure ()
Drawing usually benefits from resizing. If we want to resize, we can do this:
# figure plt.figure (figsize= (9,5)) sns.scatterplot (data=df, alpha=0.7, hue='species', size='gender')
When we resize, the legend moves to the upper left corner. Let's move the legend out of the chart so that it does not accidentally overwrite the data points:
# figure plt.figure (figsize= (9,5)) sns.scatterplot (data=df, xylene bodyweight massages, alpha=0.7, hue='species', size='gender') plt.legend (loc='upper right', bbox_to_anchor= (1.2,1))
If you want to know how to know what word combinations figsize () or bbox_to_anchor () use, you need to try which numbers are best suited for drawing.
Skill 3:sns.set_style ()
If you do not like the default style, this function helps to change the overall style of the drawing. This includes the color and background of the axis. Let's change the style to whitegrid and see how the appearance of the plot changes:
# change the default style sns.set_style ('whitegrid') # figure plt.figure (figsize= (9,5)) sns.scatterplot (data=df, xylene / bodyweight massages, alpha=0.7 / hue='species', size='gender') plt.legend (loc='upper right', bbox_to_anchor= (1.2,1))
Here are some other options to try: "darkgrid", "dark" and "ticks" to find the one you like better.
Skill 4:sns.set_context ()
In the previous figure, the label size looks very small. If you don't like the default settings, we can use sns.set_context () to change the context parameters.
I use this function mainly to control the default font size of the labels in the drawing. By changing the default values, we can save time without having to resize fonts for different elements of a single drawing (such as axis labels, headings, legends). Let's change the context to "talk" and look at the figure:
# default context change sns.set_context ('talk') # figure plt.figure (figsize= (9,5)) sns.scatterplot (data=df, xylene / bodyweight massages, alpha=0.7 / hue='species', size='gender') plt.legend (loc='upper right', bbox_to_anchor= (1.3,1))
It's easier to recognize, isn't it? Another option you can try is "poster", which will increase the default size or more.
Skill 5:sns.set_palette ()
This feature is very convenient if you want to customize the default palette to your favorite color combination. We can use the color mapping in Matplotlib. This is selected from the color library. Let's change the palette to "rainbow" and look at the figure again:
# change the default palette sns.set_palette ('rainbow') # figure plt.figure (figsize= (9,5)) sns.scatterplot (data=df, X-ray billboard massaging, alpha=0.7, hue='species', size='gender') plt.legend (loc='upper right', bbox_to_anchor= (1.3,1))
If you can't find your favorite Matplotlib color mapping, you can manually select colors to create your own unique palette. One way to create your own palette is to pass a list of color names to the function, as shown in the following example. This link is a list of color names: https://matplotlib.org/3.1.0/gallery/color/named_colors.html.
# change the default palette sns.set_palette (['green',' purple', 'red']) # figure plt.figure (figsize= (9,5)) sns.scatterplot (data=df, X-ray billboard massages, alpha=0.7, hue='species', size='gender') plt.legend (loc='upper right', bbox_to_anchor= (1.3,1))
If the color name doesn't capture what you're looking for, you can use hexadecimal colors to build your own palette to access a wider range of options (more than 16 million colors!). Here is my favorite resource, you can find a custom color palette in hexadecimal. Let's look at an example:
# change the default color palette sns.set_palette (['# 62C370mm,'# FFD166','# EF476F']) # figure plt.figure (figsize= (9,5)) sns.scatterplot (data=df, x-shaped bodyweight massages, alpha=0.7, hue='species', size='gender') plt.legend (loc='upper right', bbox_to_anchor= (1.3,1)
Skill 6:sns.set ()
From the previous three tips, I hope you can find your favorite combination (in some cases, it may retain the default setting). If we want to update the default settings for the chart, it is best to update it after importing the visualization package. This means that we will have a clip at the beginning of the script:
# Import package import matplotlib.pyplot as plt import seaborn as sns # change the default value sns.set_style ('whitegrid') sns.set_context (' talk') sns.set_palette ('rainbow')
You can update multiple default values above with sns.set (). Here is a concise version of the same code:
# Import package import matplotlib.pyplot as plt import seaborn as sns # change the default value sns.set (style='whitegrid', context='talk', palette='rainbow')
Here are six techniques. The following is a comparison of the figures before and after the adjustment:
This is the end of the content of "what are the skills for using Seaborn". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.