当前位置:网站首页>Hands on data analysis unit 2 section 4 data visualization

Hands on data analysis unit 2 section 4 data visualization

2022-06-23 17:17:00 Cangye 2021

hands-on-data-analysis Unit two Section 4 data visualization

1. Simple drawing

1.1. Import library

#inline Indicates embedding a chart into Notebook in 
%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

1.2. Basic drawing examples

import numpy as np
data  = np.arange(10)
data
plt.plot(data)

 Insert picture description here

1.3. Examples of subgraphs

fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
ax3 = fig.add_subplot(2,2,3)
ax4 = fig.add_subplot(2,2,4)

 Insert picture description here

1.4. Example of subgraph drawing

fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
ax3 = fig.add_subplot(2,2,3)
#k-- Is an option to draw black segment lines 
plt.plot(np.random.randn(50).cumsum(),'k--')
_ = ax1.hist(np.random.randn(100),bins=20,color='k',alpha=0.3)
ax2.scatter(np.arange(30),np.arange(30)+3*np.random.randn(30))

 Insert picture description here

1.5. pyplot.subplots Options

Parameters describe
nrows Row number of subgraphs
ncols Number of columns in a subgraph
sharex All subgraphs use the same x Axis scale ( adjustment xlim Will affect all subgraphs )
sharey All subgraphs use the same y Axis scale ( adjustment ylim Will affect all subgraphs )
subplot_kw Pass in add_subplot Keyword parameter Dictionary of , Used to generate subgraphs
**fig_kw Additional keyword parameters used when generating images , for example plt.subplot(2,2,figsize(8,6))

2. Visually display the distribution of survival numbers between men and women in the Titanic data set

sex = text.groupby('Sex')['Survived'].sum()
sex.plot.bar()
plt.title('survived_count')
plt.show()

 Insert picture description here

3. Visually display the proportion of survival and death of men and women in the Titanic data set

text.groupby(['Sex','Survived'])['Survived'].count().unstack().plot(kind='bar',stacked='True')
plt.title('survived_count')
plt.ylabel('count')

 Insert picture description here

4. Visually display the distribution of survival and death toll of people with different ticket prices in the Titanic data set .

#  Calculate the number of survival and death in different ticket prices  1 It means survival ,0 It means death 
fare_sur = text.groupby(['Fare'])['Survived'].value_counts().sort_values(ascending=False)
fare_sur
fig = plt.figure(figsize=(20, 18))
fare_sur.plot(grid=True)
plt.legend()
plt.show()

 Insert picture description here

5. Visually display the distribution of survival and death of people at different positions in the Titanic data set

# 1 It means survival ,0 It means death 
pclass_sur = text.groupby(['Pclass'])['Survived'].value_counts()
pclass_sur
import seaborn as sns
sns.countplot(x="Pclass", hue="Survived", data=text)

 Insert picture description here

6. Visually display the distribution of life and death at different ages in the Titanic data set

facet = sns.FacetGrid(text, hue="Survived",aspect=3)
facet.map(sns.kdeplot,'Age',shade= True)
facet.set(xlim=(0, text['Age'].max()))
facet.add_legend()

 Insert picture description here

7. Visually display the age distribution of people in different positions in the Titanic dataset .

text.Age[text.Pclass == 1].plot(kind='kde')
text.Age[text.Pclass == 2].plot(kind='kde')
text.Age[text.Pclass == 3].plot(kind='kde')
plt.xlabel("age")
plt.legend((1,2,3),loc="best")

 Insert picture description here

原网站

版权声明
本文为[Cangye 2021]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231631120307.html