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

What are the skills in python drawing

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

Share

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

This article mainly explains "what are the skills in python drawing". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the skills in python drawing".

Dataset:

Let's import the package and update the default settings for the chart to add a little personal style to the chart. We will use Seaborn's built-in dataset at the prompt:

Import seaborn as sns # v0.11.2 import matplotlib.pyplot as plt # v3.4.2 sns.set (style='darkgrid', context='talk', palette='rainbow') df = sns.load\ _ dataset ('tips') df.head ()

Tip 1: plt.subplots ()

An easy way to draw multiple subgraphs is to use plt.subplots ().

This is an example syntax for drawing two parallel subgraphs:

Fig, ax= plt.subplots (nrows=1, ncols=2, figsize= (10Jing 4)) sns.histplot (data=df, X-ray, ax=ax [0]) sns.boxplot (data=df, x-ray, ax=ax [1])

Here, we draw two subgraphs in one graph. We can further customize each subgraph.

For example, we can add a title to each subgraph like this:

Fig, ax= plt.subplots (1, 2, figsize= (10Magazine 4)) sns.histplot (data=df, xanthomorphtipies, ax=ax [0]) ax [0] .set\ _ title ("Histogram") sns.boxplot (data=df, xymorphtipies, ax=ax [1]) ax [1] .set\ _ title ("Boxplot")

All numeric variables are represented by the same set of graphs in a loop:

Numerical = df.select\ _ dtypes ('number'). Columnsfor col in numerical: fig, ax= plt.subplots (1,2, figsize= (10Magazine 4)) sns.histplot (data=df, x=col, ax=ax [0]) sns.boxplot (data=df, x=col, ax=ax [1]); technique 2: plt.subplot ()

Another way to visualize multiple graphics is to use plt.subplot (), with no s at the end

The syntax is slightly different from before:

Plt.figure (figsize= (10)) ax1 = plt.subplot (1) sns.histplot (data=df, x-ray recording, ax=ax1) ax2 = plt.subplot (1-line 2) sns.boxplot (data=df, x-ray recording, ax=ax2)

This method is especially useful when we want to draw the same type of graphics for multiple diagrams and view all of them in a single diagram:

Plt.figure (figsize= (145.4)) for I, col in enumerate (numerical): ax= plt.subplot (1, len (numerical), iTunes 1) sns.boxplot (data=df, x=col, ax=ax)

We can also customize sub-graphics. For example, add a title

Plt.figure (figsize= (145.4)) for I, col in enumerate (numerical): ax= plt.subplot (1, len (numerical), iTun1) sns.boxplot (data=df, x=col, ax=ax) ax.set\ _ title (f "Boxplot of {col}")

Through the comparison below, we can better understand their similarities and differences. It is useful to be familiar with these two methods, because they can be used in different situations.

Tip 3: plt.tight_layout ()

When drawing multiple graphs, it is common to see the labels of some subgraphs overlap on their adjacent subgraphs.

As follows:

Categorical = df.select\ _ dtypes ('category') .columnsplt.figure (figsize= (8,8)) for I, col in enumerate (categorical): ax= plt.subplot (2,2, iTun1) sns.countplot (data=df, x=col, ax=ax)

The variable names on the x-axis of the top two charts are cut out, and the y-axis label of the right chart overlaps the left subgraph. It is very convenient to use plt.tight_layout

Plt.figure (figsize= (8,8) for I, col in enumerate (categorical): ax= plt.subplot (2,2,1) sns.countplot (data=df, x=col, ax=ax) plt.tight\ _ layout ()

The major looks better.

Tip 4: plt.suptitle ()

Add a title to the real drawing:

Plt.figure (figsize= (8,8) for I, col in enumerate (categorical): ax= plt.subplot (2,2,1) sns.countplot (data=df, x=col, ax=ax) plt.suptitle ('Category counts for all categorical variables') plt.tight\ _ layout ()

In addition, you can customize each graph according to your preferences. For example, you can still add a title to each subgraph.

Thank you for your reading, these are the contents of "what are the skills in python drawing?" after the study of this article, I believe you have a deeper understanding of the skills in python drawing, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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