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 does python display several charts together on the same picture?

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how python displays several charts together on the same picture". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how python displays several charts together on the same picture".

1: how to display several charts together on the same picture? Key code import matplotlib.pyplot as plt

# set figure_size size

Plt.rcParams ['figure.figsize'] = (8.0,6.0)

Fig = plt.figure ()

# set chart color

Fig.set (alpha=0.2)

# the first small picture

Plt.subplot2grid ((2) 3), (0))

Data_train ['Survived']. Value_counts (). Plot (kind='bar')

Plt.ylabel (u "number")

Plt.title (u "crew rescued (1 is rescued)")

# the second small picture

Plt.subplot2grid ((2) 3), (0) 1))

Data_train ['Pclass'] .value_counts () .plot (kind= "bar")

Plt.ylabel (u "number")

Plt.title (u "passenger class distribution")

# the third small picture

Plt.subplot2grid ((2) 3), (0) 2)

Plt.scatter (data_train ['Survived'], data_train [' Age'])

Plt.ylabel (u "age")

Plt.grid (b=True, which='major', axis='y')

Plt.title (u "Distribution of rescued by age (1 for rescued)")

# the fourth small picture, distribution map

Plt.subplot2grid ((2) 3), (1) 0), colspan=2)

Plot [data_ _ train.Pclass = = 1] .plot (kind='kde')

Plot [data_ _ train.Pclass = = 2] .plot (kind='kde')

Plot [data_ _ train.Pclass = = 3] .plot (kind='kde')

Plt.xlabel (u "age")

Plt.ylabel (u "density")

Plt.title (u "age distribution of passengers by class")

Plt.legend ((u`first class', Upright 2', Utre3'), loc='best')

# the fifth mini-picture

Plt.subplot2grid ((2) 3), (1) 2)

Data_train.Embarked.value_counts () plot (kind='bar')

Plt.title (u "number of people boarding at each port of embarkation")

Plt.ylabel (u "number")

Plt.show ()

We can see from the above visualization results that we can actually see some rules, for example, the chance of survival is greater than that of death, and then there is little difference in age between the people who are rescued, and then the rich (those who fly first class) will be too old.

2: how to use sklearn polynomials to derive more variables?

In fact, you should have heard of the theory about this way of deriving variables a long time ago, but how to implement it in Python, that is, to share it with you here today, is actually very simple, that is, to call the PolynomialFeatures method of sklearn. For details, you can take a look at the demo below.

Here we use a human body acceleration data set, that is, to record the acceleration of a person in different directions when doing different actions, which have three directions, named x, y, z.

Key code # extends numerical features

From sklearn.preprocessing import PolynomialFeatures

X = df [['x, y, I, I]]

Y = df ['activity']

Poly = PolynomialFeatures (degree=2, include_bias=False, interaction_only=False)

X_poly = poly.fit_transform (x)

Pd.DataFrame (x_poly, columns=poly.get_feature_names ()). Head ()

By simply calling it like this, you can generate a lot of new variables.

3: how to modify the distribution to quasi-normal distribution?

Today we are using a new data set, which is also a competition on kaggle. You can download it first:

Import pandas as pd

Import numpy as np

# Plots

Import seaborn as sns

Import matplotlib.pyplot as plt

# read dataset

Train = pd.read_csv ('. / data/house-prices-advanced-regression-techniques/train.csv')

Train.head ()

First of all, this is a topic of price forecasting. Before we start, we need to look at the distribution. We can call the following methods to draw:

Sns.set_style ("white")

Sns.set_color_codes (palette='deep')

F, ax = plt.subplots (figsize= (8,7))

# Check the new distribution

Sns.distplot (train ['SalePrice'], color= "b")

Ax.xaxis.grid (False)

Ax.set (ylabel= "Frequency")

Ax.set (xlabel= "SalePrice")

Ax.set (title= "SalePrice distribution")

Sns.despine (trim=True, left=True)

Plt.show ()

We can see from the results that the sales price is skewed to the right, and most machine learning models can not deal with non-normal distribution data well, so we can apply log (1x) transformation to correct it. So how exactly can we implement it in Python code?

# log (1roomx) conversion

Train ["SalePrice_log"] = np.log1p (train ["SalePrice"])

Sns.set_style ("white")

Sns.set_color_codes (palette='deep')

F, ax = plt.subplots (figsize= (8,7))

Sns.distplot (train ['SalePrice_log'], fit=norm, color= "b")

# get the parameters of normal distribution

(mu, sigma) = norm.fit (train ['SalePrice_log'])

Plt.legend (['Normal dist. ($\ mu=$ {: .2f} and $\ sigma=$ {: .2f})' .format (mu, sigma)]

Loc='best')

Ax.xaxis.grid (False)

Ax.set (ylabel= "Frequency")

Ax.set (xlabel= "SalePrice")

Ax.set (title= "SalePrice distribution")

Sns.despine (trim=True, left=True)

Plt.show ()

At this point, I believe you have a deeper understanding of "how python displays several charts together on the same chart". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue 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.

Share To

Internet Technology

Wechat

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

12
Report