第9章 可视化综合实训

9.1 实训1 python纵向柱状图实训

import pandas as pd
from matplotlib import pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams[ 'font.sans-serif'] =[ 'Microsoft YaHei'] #设置字体
import xlrd
import seaborn as sns
plt.title("男女爱好人数分布图"); #图标题
num = np.array([14325, 9403, 13227, 18651])
ratio = np.array([0.75, 0.6, 0.22, 0.1])
men = num * ratio
women = num * (1-ratio)
x = ['足球','游泳','看剧','逛街']
width = 0.5
idx = np.arange(len(x))
plt.bar(idx, men, width, color='red', label='男性用户')
plt.bar(idx, women, width,bottom=men, color='purple', label='女性用户')  #这一块可设置bottom,top,如果是水平放置的,可以设置right或者left。
plt.xlabel('应用类别')
plt.ylabel('男女分布')
plt.xticks(idx+width/2, x, rotation=30)
#bar图上显示数字
for a,b in zip(idx,men):
    plt.text(a, b+0.05, '%.0f' % b, ha='center', va= 'bottom',fontsize=12,color="white")
for a,b,c in zip(idx,women,men):
    plt.text(a, b+c+0.5, '%.0f' % b, ha='center', va= 'bottom',fontsize=12)
plt.legend()
plt.show()

9.2 实训2 python水平柱状图实训

import pandas as pd
from matplotlib import pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
import xlrd
plt.rcParams[ 'font.sans-serif'] =[ 'Microsoft YaHei'] #设置字体

name=['图片URL','商品标题','折扣价','原价','地址','销量','商品链接']
df = pd.read_excel('list.xlsx',header=1,names=name)
df = df[['商品标题','原价']]
data = df.sort_values(by=['原价'],ascending=False)
data.index = data['商品标题']
var = data['原价'][:10]
plt.figure(figsize=(10, 5))
plt.title('原价最贵的物品',fontsize=18)
plt.xlabel('原价',fontsize=10)
plt.ylabel('商品标题',fontsize=10)
var.plot(kind='barh', stacked=True,alpha=0.7,color=['blue'])
plt.show()

数据集list.xlsx

9.3 实训3 Python多数据并列柱状图实训

import pandas as pd
from matplotlib import pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams[ 'font.sans-serif'] =[ 'Microsoft YaHei'] #设置字体

plt.title("不同学校专业招生人数分布图"); #图标题
x=np.arange(5)
y=[400,170,160,90,50]
y1=[300,180,150,70,90]
bar_width=0.5
tick_label=["计算机","机械","电子","管理","物理"]
plt.bar(x,y,bar_width,color="r",align="center",label="学校A")
plt.bar(x+bar_width,y1,bar_width,color="y",align="center",label="学校B")
plt.xlabel("专业")
plt.ylabel("招生人数")
plt.xticks(x+bar_width/2,tick_label)
plt.legend()
plt.show()

9.4 实训4 python折线图实训

import pandas as pd
from matplotlib import pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams[ 'font.sans-serif'] =[ 'Microsoft YaHei'] #设置字体

x1 = ['2019-01', '2019-02', '2019-03', '2019-04', '2019-05', '2019-06', '2019-07', '2019-08',
      '2019-09', '2019-10', '2019-11', '2019-12']
y1 = [9700, 9800, 9900, 12000, 11000, 12400, 13000, 13400, 14000, 14100, 13900, 13700]
plt.figure(figsize=(10, 8))
# 标题
plt.title("房价变化")
plt.plot(x1, y1, label='房价变化', linewidth=2, color='r', marker='o',
         markerfacecolor='blue', markersize=10)
# 横坐标描述
plt.xlabel('月份')
# 纵坐标描述
plt.ylabel('房价')
for a, b in zip(x1, y1):
    plt.text(a, b, b, ha='center', va='bottom', fontsize=10)
plt.legend()
plt.show()

9.5 实训5 Python散点图实训

import pandas as pd
from matplotlib import pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams[ 'font.sans-serif'] =[ 'Microsoft YaHei'] #设置字体

x=np.random.randn(10)
y=np.random.randn(10)
plt.scatter(x,y,s=np.power(20*x+30*y,2),
c=np.random.rand(10),marker="o")
plt.show()

9.6 实训6 机器学习中的可视化应用

import pandas as pd
from matplotlib import pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
plt.rcParams[ 'font.sans-serif'] =[ 'Microsoft YaHei'] #设置字体

sns.set(style='ticks',color_codes=True)
iris = sns.load_dataset('iris')
s =sns.pairplot(iris,hue='species')
plt.show()

Copyright © ZHOUWEN all right reserved,powered by GitbookLatest update: 2021-05-31 19:49:33

results matching ""

    No results matching ""