python数据可视化案例2017年6省gdp_吴裕雄 数据挖掘与分析案例实战(5)——python数据可视化...

# 饼图的绘制

# 导入第三方模块

import matplotlib

import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif']=['Simhei']

plt.rcParams['axes.unicode_minus']=False

ziti = matplotlib.font_manager.FontProperties(fname='C:\Windows\Fonts\simsun.ttc')

# 构造数据

edu = [0.2515,0.3724,0.3336,0.0368,0.0057]

labels = ['中专','大专','本科','硕士','其他']

# 绘制饼图

plt.pie(x = edu, # 绘图数据

labels=labels, # 添加教育水平标签

autopct='%.1f%%' # 设置百分比的格式,这里保留一位小数

)

# 添加图标题

plt.title('失信用户的教育水平分布')

# 显示图形

plt.show()

99a633208ec0e907f16a241117133cbe71a.png

# 构造数据

edu = [0.2515,0.3724,0.3336,0.0368,0.0057]

labels = ['中专','大专','本科','硕士','其他']

# 添加修饰的饼图

explode = [0,0.1,0,0,0] # 生成数据,用于突出显示大专学历人群

colors=['#9999ff','#ff9999','#7777aa','#2442aa','#dd5555'] # 自定义颜色

# 中文乱码和坐标轴负号的处理

plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']

plt.rcParams['axes.unicode_minus'] = False

# 将横、纵坐标轴标准化处理,确保饼图是一个正圆,否则为椭圆

plt.axes(aspect='equal')

# 绘制饼图

plt.pie(x = edu, # 绘图数据

explode=explode, # 突出显示大专人群

labels=labels, # 添加教育水平标签

colors=colors, # 设置饼图的自定义填充色

autopct='%.1f%%', # 设置百分比的格式,这里保留一位小数

pctdistance=0.8, # 设置百分比标签与圆心的距离

labeldistance = 1.1, # 设置教育水平标签与圆心的距离

startangle = 180, # 设置饼图的初始角度

radius = 1.2, # 设置饼图的半径

counterclock = False, # 是否逆时针,这里设置为顺时针方向

wedgeprops = {'linewidth': 1.5, 'edgecolor':'green'},# 设置饼图内外边界的属性值

textprops = {'fontsize':10, 'color':'black'}, # 设置文本标签的属性值

)

# 添加图标题

plt.title('失信用户的受教育水平分布')

# 显示图形

plt.show()

672250633f682872d03a91bc8948594a200.png

# 导入第三方模块

import pandas as pd

import matplotlib.pyplot as plt

# 构建序列

data1 = pd.Series({'中专':0.2515,'大专':0.3724,'本科':0.3336,'硕士':0.0368,'其他':0.0057})

print(data1)

data1.name = ''

# 控制饼图为正圆

plt.axes(aspect = 'equal')

# plot方法对序列进行绘图

data1.plot(kind = 'pie', # 选择图形类型

autopct='%.1f%%', # 饼图中添加数值标签

radius = 1, # 设置饼图的半径

startangle = 180, # 设置饼图的初始角度

counterclock = False, # 将饼图的顺序设置为顺时针方向

title = '失信用户的受教育水平分布', # 为饼图添加标题

wedgeprops = {'linewidth': 1.5, 'edgecolor':'green'}, # 设置饼图内外边界的属性值

textprops = {'fontsize':10, 'color':'black'} # 设置文本标签的属性值

)

# 显示图形

plt.show()

59774e0e4fd48719f4cf2c3f535da8cf2c6.png

# 条形图的绘制--垂直条形图

# 读入数据

GDP = pd.read_excel(r'F:\\python_Data_analysis_and_mining\\06\\Province GDP 2017.xlsx')

# 设置绘图风格(不妨使用R语言中的ggplot2风格)

plt.style.use('ggplot')

# 绘制条形图

plt.bar(left = range(GDP.shape[0]), # 指定条形图x轴的刻度值

height = GDP.GDP, # 指定条形图y轴的数值

tick_label = GDP.Province, # 指定条形图x轴的刻度标签

color = 'steelblue', # 指定条形图的填充色

)

# 添加y轴的标签

plt.ylabel('GDP(万亿)')

# 添加条形图的标题

plt.title('2017年度6个省份GDP分布')

# 为每个条形图添加数值标签

for x,y in enumerate(GDP.GDP):

plt.text(x,y+0.1,'%s' %round(y,1),ha='center')

# 显示图形

plt.show()

d0e37d4be6cd3bded00ac8169074b0984ee.png

# 条形图的绘制--水平条形图

# 对读入的数据作升序排序

GDP.sort_values(by = 'GDP', inplace = True)

# 绘制条形图

plt.barh(bottom = range(GDP.shape[0]), # 指定条形图y轴的刻度值

width = GDP.GDP, # 指定条形图x轴的数值

tick_label = GDP.Province, # 指定条形图y轴的刻度标签

color = 'steelblue', # 指定条形图的填充色

)

# 添加x轴的标签

plt.xlabel('GDP(万亿)')

# 添加条形图的标题

plt.title('2017年度6个省份GDP分布')

# 为每个条形图添加数值标签

for y,x in enumerate(GDP.GDP):

plt.text(x+0.1,y,'%s' %round(x,1),va='center')

# 显示图形

plt.show()

f459f8bc127562891c2f8c66590ab04fe47.png

# 条形图的绘制--堆叠条形图

# 读入数据

Industry_GDP = pd.read_excel(r'F:\\python_Data_analysis_and_mining\\06\\Industry_GDP.xlsx')

print(Industry_GDP.head())

# 取出四个不同的季度标签,用作堆叠条形图x轴的刻度标签

Quarters = Industry_GDP.Quarter.unique()

print(Quarters)

# 取出第一产业的四季度值

Industry1 = Industry_GDP.GPD[Industry_GDP.Industry_Type == '第一产业']

print(Industry1)

# 重新设置行索引

Industry1.index = range(len(Quarters))

print(Industry1)

# 取出第二产业的四季度值

Industry2 = Industry_GDP.GPD[Industry_GDP.Industry_Type == '第二产业']

print(Industry2)

# 重新设置行索引

Industry2.index = range(len(Quarters))

print(Industry2)

# 取出第三产业的四季度值

Industry3 = Industry_GDP.GPD[Industry_GDP.Industry_Type == '第三产业']

print(Industry3)

# 绘制堆叠条形图

# 各季度下第一产业的条形图

plt.bar(left = range(len(Quarters)), height=Industry1, color = 'steelblue', label = '第一产业', tick_label = Quarters)

# 各季度下第二产业的条形图

plt.bar(left = range(len(Quarters)), height=Industry2, bottom = Industry1, color = 'green', label = '第二产业')

# 各季度下第三产业的条形图

plt.bar(left = range(len(Quarters)), height=Industry3, bottom = Industry1 + Industry2, color = 'red', label = '第三产业')

# 添加y轴标签

plt.ylabel('生成总值(亿)')

# 添加图形标题

plt.title('2017年各季度三产业总值')

# 显示各产业的图例

plt.legend()

# 显示图形

plt.show()

2cbad91c8bfbedfbd5a6524ec615888b181.png

5a075a73293fc270c487bf8e08379e47df6.png

# Pandas模块之垂直或水平条形图

# 绘图(此时的数据集在前文已经按各省GDP做过升序处理)

GDP.GDP.plot(kind = 'bar', width = 0.8, rot = 0, color = 'steelblue', title = '2017年度6个省份GDP分布')

# 添加y轴标签

plt.ylabel('GDP(万亿)')

# 添加x轴刻度标签

plt.xticks(range(len(GDP.Province)), #指定刻度标签的位置

GDP.Province # 指出具体的刻度标签值

)

# 为每个条形图添加数值标签

for x,y in enumerate(GDP.GDP):

plt.text(x-0.1,y+0.2,'%s' %round(y,1),va='center')

# 显示图形

plt.show()

85cc30bbb20bb72925021da6050c44ae139.png

# 条形图的绘制--水平交错条形图

# 导入第三方模块

import numpy as np

# 读入数据

HuRun = pd.read_excel(r'F:\\python_Data_analysis_and_mining\\06\\HuRun.xlsx')

# 取出城市名称

Cities = HuRun.City.unique()

# 取出2016年各城市亿万资产家庭数

Counts2016 = HuRun.Counts[HuRun.Year == 2016]

# 取出2017年各城市亿万资产家庭数

Counts2017 = HuRun.Counts[HuRun.Year == 2017]

# 绘制水平交错条形图

bar_width = 0.4

plt.bar(left = np.arange(len(Cities)), height = Counts2016, label = '2016', color = 'steelblue', width = bar_width)

plt.bar(left = np.arange(len(Cities))+bar_width, height = Counts2017, label = '2017', color = 'indianred', width = bar_width)

# 添加刻度标签(向右偏移0.225)

plt.xticks(np.arange(5)+0.2, Cities)

# 添加y轴标签

plt.ylabel('亿万资产家庭数')

# 添加图形标题

plt.title('近两年5个城市亿万资产家庭数比较')

# 添加图例

plt.legend()

# 显示图形

plt.show()

# Pandas模块之水平交错条形图

HuRun_reshape = HuRun.pivot_table(index = 'City', columns='Year', values='Counts').reset_index()

# 对数据集降序排序

HuRun_reshape.sort_values(by = 2016, ascending = False, inplace = True)

HuRun_reshape.plot(x = 'City', y = [2016,2017], kind = 'bar', color = ['steelblue', 'indianred'],

rot = 0, # 用于旋转x轴刻度标签的角度,0表示水平显示刻度标签

width = 0.8, title = '近两年5个城市亿万资产家庭数比较')

# 添加y轴标签

plt.ylabel('亿万资产家庭数')

plt.xlabel('')

plt.show()

# seaborn模块之垂直或水平条形图

# 导入第三方模块

import seaborn as sns

sns.barplot(y = 'Province', # 指定条形图x轴的数据

x = 'GDP', # 指定条形图y轴的数据

data = GDP, # 指定需要绘图的数据集

color = 'steelblue', # 指定条形图的填充色

orient = 'horizontal' # 将条形图水平显示

)

# 重新设置x轴和y轴的标签

plt.xlabel('GDP(万亿)')

plt.ylabel('')

# 添加图形的标题

plt.title('2017年度6个省份GDP分布')

# 为每个条形图添加数值标签

for y,x in enumerate(GDP.GDP):

plt.text(x,y,'%s' %round(x,1),va='center')

# 显示图形

plt.show()

3c387f1385866b56517478a1468099867ed.png

# 读入数据

Titanic = pd.read_csv(r'F:\\python_Data_analysis_and_mining\\06\\titanic_train.csv')

print(Titanic.shape)

# 绘制水平交错条形图

sns.barplot(x = 'Pclass', # 指定x轴数据

y = 'Age', # 指定y轴数据

hue = 'Sex', # 指定分组数据

data = Titanic, # 指定绘图数据集

palette = 'RdBu', # 指定男女性别的不同颜色

errcolor = 'blue', # 指定误差棒的颜色

errwidth=2, # 指定误差棒的线宽

saturation = 1, # 指定颜色的透明度,这里设置为无透明度

capsize = 0.05 # 指定误差棒两端线条的宽度

)

# 添加图形标题

plt.title('各船舱等级中男女乘客的年龄差异')

# 显示图形

plt.show()

387855eec2fbbd4945c70f1cb123a7351ed.png

# 读入数据

Titanic = pd.read_csv(r'F:\\python_Data_analysis_and_mining\\06\\titanic_train.csv')

# matplotlib模块绘制直方图

# 检查年龄是否有缺失

print(any(Titanic.Age.isnull()))

# 不妨删除含有缺失年龄的观察

Titanic.dropna(subset=['Age'], inplace=True)

# 绘制直方图

plt.hist(x = Titanic.Age, # 指定绘图数据

bins = 20, # 指定直方图中条块的个数

color = 'steelblue', # 指定直方图的填充色

edgecolor = 'black' # 指定直方图的边框色

)

# 添加x轴和y轴标签

plt.xlabel('年龄')

plt.ylabel('频数')

# 添加标题

plt.title('乘客年龄分布')

# 显示图形

plt.show()

baa1d2e3d7112da0eb565544de4f7f3c9cc.png

# Pandas模块绘制直方图和核密度图

# 绘制直方图

Titanic.Age.plot(kind = 'hist', bins = 20, color = 'steelblue', edgecolor = 'black', normed = True, label = '直方图')

# 绘制核密度图

Titanic.Age.plot(kind = 'kde', color = 'red', label = '核密度图')

# 添加x轴和y轴标签

plt.xlabel('年龄')

plt.ylabel('核密度值')

# 添加标题

plt.title('乘客年龄分布')

# 显示图例

plt.legend()

# 显示图形

plt.show()

1020928-20181203164725075-838994298.png

# seaborn模块绘制分组的直方图和核密度图

# 取出男性年龄

Age_Male = Titanic.Age[Titanic.Sex == 'male']

# 取出女性年龄

Age_Female = Titanic.Age[Titanic.Sex == 'female']

# 绘制男女乘客年龄的直方图

sns.distplot(Age_Male, bins = 20, kde = False, hist_kws = {'color':'steelblue'}, label = '男性')

# 绘制女性年龄的直方图

sns.distplot(Age_Female, bins = 20, kde = False, hist_kws = {'color':'purple'}, label = '女性')

plt.title('男女乘客的年龄直方图')

# 显示图例

plt.legend()

# 显示图形

plt.show()

8844395612dad03ab1ce3279b39c8c14734.png

# 绘制男女乘客年龄的核密度图

sns.distplot(Age_Male, hist = False, kde_kws = {'color':'red', 'linestyle':'-'},

norm_hist = True, label = '男性')

# 绘制女性年龄的核密度图

sns.distplot(Age_Female, hist = False, kde_kws = {'color':'black', 'linestyle':'--'},

norm_hist = True, label = '女性')

plt.title('男女乘客的年龄核密度图')

# 显示图例

plt.legend()

# 显示图形

plt.show()

1020928-20181203164820718-1755160589.png

import pandas as pd

import matplotlib.pyplot as plt

# 读取数据

Sec_Buildings = pd.read_excel(r'F:\\python_Data_analysis_and_mining\\06\\sec_buildings.xlsx')

print(Sec_Buildings.shape)

# 绘制箱线图

plt.boxplot(x = Sec_Buildings.price_unit, # 指定绘图数据

patch_artist=True, # 要求用自定义颜色填充盒形图,默认白色填充

showmeans=True, # 以点的形式显示均值

boxprops = {'color':'black','facecolor':'steelblue'}, # 设置箱体属性,如边框色和填充色

# 设置异常点属性,如点的形状、填充色和点的大小

flierprops = {'marker':'o','markerfacecolor':'red', 'markersize':3},

# 设置均值点的属性,如点的形状、填充色和点的大小

meanprops = {'marker':'D','markerfacecolor':'indianred', 'markersize':4},

# 设置中位数线的属性,如线的类型和颜色

medianprops = {'linestyle':'--','color':'orange'},

labels = [''] # 删除x轴的刻度标签,否则图形显示刻度标签为1

)

# 添加图形标题

plt.title('二手房单价分布的箱线图')

# 显示图形

plt.show()

b1925507fc5e7e18acc5be1bb1d443caf38.png

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

# 读取数据

Sec_Buildings = pd.read_excel(r'F:\\python_Data_analysis_and_mining\\06\\sec_buildings.xlsx')

print(Sec_Buildings.shape)

# 二手房在各行政区域的平均单价

group_region = Sec_Buildings.groupby('region')

print(group_region)

avg_price = group_region.aggregate({'price_unit':np.mean}).sort_values('price_unit', ascending = False)

print(avg_price)

# 通过循环,将不同行政区域的二手房存储到列表中

region_price = []

for region in avg_price.index:

region_price.append(Sec_Buildings.price_unit[Sec_Buildings.region == region])

# print(region_price)

# 绘制分组箱线图

plt.boxplot(x = region_price,

patch_artist=True,

labels = avg_price.index, # 添加x轴的刻度标签

showmeans=True,

boxprops = {'color':'black', 'facecolor':'steelblue'},

flierprops = {'marker':'o','markerfacecolor':'red', 'markersize':3},

meanprops = {'marker':'D','markerfacecolor':'indianred', 'markersize':4},

medianprops = {'linestyle':'--','color':'orange'}

)

# 添加y轴标签

plt.ylabel('单价(元)')

# 添加标题

plt.title('不同行政区域的二手房单价对比')

# 显示图形

plt.show()

f7b065ec015ae56003ce7aa3da76887c986.png

1020928-20181203164940750-1764942472.png

import seaborn as sns

# 绘制分组箱线图

sns.boxplot(x = 'region', y = 'price_unit', data = Sec_Buildings,

order = avg_price.index, showmeans=True,color = 'steelblue',

flierprops = {'marker':'o','markerfacecolor':'red', 'markersize':3},

meanprops = {'marker':'D','markerfacecolor':'indianred', 'markersize':4},

medianprops = {'linestyle':'--','color':'orange'}

)

# 更改x轴和y轴标签

plt.xlabel('')

plt.ylabel('单价(元)')

# 添加标题

plt.title('不同行政区域的二手房单价对比')

# 显示图形

plt.show()

1020928-20181203165005650-13894088.png

# 读取数据

tips = pd.read_csv(r'F:\\python_Data_analysis_and_mining\\06\\tips.csv')

print(tips.shape)

# 绘制分组小提琴图

sns.violinplot(x = "total_bill", # 指定x轴的数据

y = "day", # 指定y轴的数据

hue = "sex", # 指定分组变量

data = tips, # 指定绘图的数据集

order = ['Thur','Fri','Sat','Sun'], # 指定x轴刻度标签的顺序

scale = 'count', # 以男女客户数调节小提琴图左右的宽度

split = True, # 将小提琴图从中间割裂开,形成不同的密度曲线;

palette = 'RdBu' # 指定不同性别对应的颜色(因为hue参数为设置为性别变量)

)

# 添加图形标题

plt.title('每天不同性别客户的消费额情况')

# 设置图例

plt.legend(loc = 'upper center', ncol = 2)

# 显示图形

plt.show()

1020928-20181203165031285-2120271976.png

# 数据读取

wechat = pd.read_excel(r'F:\\python_Data_analysis_and_mining\\06\\wechat.xlsx')

print(wechat.shape)

# 绘制单条折线图

plt.plot(wechat.Date, # x轴数据

wechat.Counts, # y轴数据

linestyle = '-', # 折线类型

linewidth = 2, # 折线宽度

color = 'steelblue', # 折线颜色

marker = 'o', # 折线图中添加圆点

markersize = 6, # 点的大小

markeredgecolor='black', # 点的边框色

markerfacecolor='brown') # 点的填充色

# 添加y轴标签

plt.ylabel('人数')

# 添加图形标题

plt.title('每天微信文章阅读人数趋势')

# 显示图形

plt.show()

c52a40f0b486a9c9d9c133f1100c1197caf.png

# 绘制两条折线图

# 导入模块,用于日期刻度的修改

import matplotlib as mpl

# 绘制阅读人数折线图

plt.plot(wechat.Date, # x轴数据

wechat.Counts, # y轴数据

linestyle = '-', # 折线类型,实心线

color = 'steelblue', # 折线颜色

label = '阅读人数'

)

# 绘制阅读人次折线图

plt.plot(wechat.Date, # x轴数据

wechat.Times, # y轴数据

linestyle = '--', # 折线类型,虚线

color = 'indianred', # 折线颜色

label = '阅读人次'

)

plt.show()

1020928-20181203165128395-455457630.png

import matplotlib as mpl

# 获取图的坐标信息

ax = plt.gca()

# 设置日期的显示格式

date_format = mpl.dates.DateFormatter("%m-%d")

ax.xaxis.set_major_formatter(date_format)

# 设置x轴显示多少个日期刻度

# xlocator = mpl.ticker.LinearLocator(10)

# 设置x轴每个刻度的间隔天数

xlocator = mpl.ticker.MultipleLocator(7)

ax.xaxis.set_major_locator(xlocator)

# 为了避免x轴刻度标签的紧凑,将刻度标签旋转45度

plt.xticks(rotation=45)

# 添加y轴标签

plt.ylabel('人数')

# 添加图形标题

plt.title('每天微信文章阅读人数与人次趋势')

# 添加图例

plt.legend()

# 显示图形

plt.show()

# 读取天气数据

weather = pd.read_excel(r'F:\\python_Data_analysis_and_mining\\06\\weather.xlsx')

# 统计每月的平均最高气温

data = weather.pivot_table(index = 'month', columns='year', values='high')

# 绘制折线图

data.plot(kind = 'line',

style = ['-','--',':'] # 设置折线图的线条类型

)

# 修改x轴和y轴标签

plt.xlabel('月份')

plt.ylabel('气温')

# 添加图形标题

plt.title('每月平均最高气温波动趋势')

# 显示图形

plt.show()

421c20e8a2fc537fc79a00ec6fe90c8948b.png

# 读入数据

iris = pd.read_csv(r'F:\\python_Data_analysis_and_mining\\06\\iris.csv')

print(iris.shape)

# 绘制散点图

plt.scatter(x = iris.Petal_Width, # 指定散点图的x轴数据

y = iris.Petal_Length, # 指定散点图的y轴数据

color = 'steelblue' # 指定散点图中点的颜色

)

# 添加x轴和y轴标签

plt.xlabel('花瓣宽度')

plt.ylabel('花瓣长度')

# 添加标题

plt.title('鸢尾花的花瓣宽度与长度关系')

# 显示图形

plt.show()

a08b8e1fae48b3c94bf48020aebbfb95709.png

# Pandas模块绘制散点图

# 绘制散点图

iris.plot(x = 'Petal_Width', y = 'Petal_Length', kind = 'scatter', title = '鸢尾花的花瓣宽度与长度关系')

# 修改x轴和y轴标签

plt.xlabel('花瓣宽度')

plt.ylabel('花瓣长度')

# 显示图形

plt.show()

aed8162b771089fde4d7d5dd6762210a322.png

# seaborn模块绘制分组散点图

sns.lmplot(x = 'Petal_Width', # 指定x轴变量

y = 'Petal_Length', # 指定y轴变量

hue = 'Species', # 指定分组变量

data = iris, # 指定绘图数据集

legend_out = False, # 将图例呈现在图框内

truncate=True # 根据实际的数据范围,对拟合线作截断操作

)

# 修改x轴和y轴标签

plt.xlabel('花瓣宽度')

plt.ylabel('花瓣长度')

# 添加标题

plt.title('鸢尾花的花瓣宽度与长度关系')

# 显示图形

plt.show()

8e10765523fc1a1e1d87a6ab63478c44218.png

# 读取数据

Prod_Category = pd.read_excel(r'F:\\python_Data_analysis_and_mining\\06\\SuperMarket.xlsx')

print(Prod_Category.shape)

# 将利润率标准化到[0,1]之间(因为利润率中有负数),然后加上微小的数值0.001

range_diff = Prod_Category.Profit_Ratio.max()-Prod_Category.Profit_Ratio.min()

print(range_diff)

Prod_Category['std_ratio'] = (Prod_Category.Profit_Ratio-Prod_Category.Profit_Ratio.min())/range_diff + 0.001

print(Prod_Category)

# 绘制办公用品的气泡图

plt.scatter(x = Prod_Category.Sales[Prod_Category.Category == '办公用品'],

y = Prod_Category.Profit[Prod_Category.Category == '办公用品'],

s = Prod_Category.std_ratio[Prod_Category.Category == '办公用品']*1000,

color = 'steelblue', label = '办公用品', alpha = 0.6

)

# 绘制技术产品的气泡图

plt.scatter(x = Prod_Category.Sales[Prod_Category.Category == '技术产品'],

y = Prod_Category.Profit[Prod_Category.Category == '技术产品'],

s = Prod_Category.std_ratio[Prod_Category.Category == '技术产品']*1000,

color = 'indianred' , label = '技术产品', alpha = 0.6

)

# 绘制家具产品的气泡图

plt.scatter(x = Prod_Category.Sales[Prod_Category.Category == '家具产品'],

y = Prod_Category.Profit[Prod_Category.Category == '家具产品'],

s = Prod_Category.std_ratio[Prod_Category.Category == '家具产品']*1000,

color = 'black' , label = '家具产品', alpha = 0.6

)

# 添加x轴和y轴标签

plt.xlabel('销售额')

plt.ylabel('利润')

# 添加标题

plt.title('销售额、利润及利润率的气泡图')

# 添加图例

plt.legend()

# 显示图形

plt.show()

63cac98fee7906bb77e5f5386f0554e9f9a.png

9e03fef20421010941bd1979130e54eb772.png

# 读取数据

Sales = pd.read_excel(r'F:\\python_Data_analysis_and_mining\\06\\Sales.xlsx')

print(Sales.shape)

print(Sales.head())

# 根据交易日期,衍生出年份和月份字段

Sales['year'] = Sales.Date.dt.year

Sales['month'] = Sales.Date.dt.month

print(Sales.head())

# 统计每年各月份的销售总额

Summary = Sales.pivot_table(index = 'month', columns = 'year', values = 'Sales', aggfunc = np.sum)

print(Summary)

# 绘制热力图

sns.heatmap(data = Summary, # 指定绘图数据

cmap = 'PuBuGn', # 指定填充色

linewidths = .1, # 设置每个单元格边框的宽度

annot = True, # 显示数值

fmt = '.1e' # 以科学计算法显示数据

)

#添加标题

plt.title('每年各月份销售总额热力图')

# 显示图形

plt.show()

4bd24e1c3aafbaa2067b2ca5b23c993e7de.png

541b589b8266179b05a4b6422c9efd422f1.png

# 读取数据

Prod_Trade = pd.read_excel(r'F:\\python_Data_analysis_and_mining\\06\\Prod_Trade.xlsx')

print(Prod_Trade.shape)

print(Prod_Trade.head())

# 衍生出交易年份和月份字段

Prod_Trade['year'] = Prod_Trade.Date.dt.year

Prod_Trade['month'] = Prod_Trade.Date.dt.month

print(Prod_Trade.head())

# 设置大图框的长和高

plt.figure(figsize = (12,6))

# 设置第一个子图的布局

ax1 = plt.subplot2grid(shape = (2,3), loc = (0,0))

# 统计2012年各订单等级的数量

Class_Counts = Prod_Trade.Order_Class[Prod_Trade.year == 2012].value_counts()

Class_Percent = Class_Counts/Class_Counts.sum()

# 将饼图设置为圆形(否则有点像椭圆)

ax1.set_aspect(aspect = 'equal')

# 绘制订单等级饼图

ax1.pie(x = Class_Percent.values, labels = Class_Percent.index, autopct = '%.1f%%')

# 添加标题

ax1.set_title('各等级订单比例')

# 设置第二个子图的布局

ax2 = plt.subplot2grid(shape = (2,3), loc = (0,1))

# 统计2012年每月销售额

Month_Sales = Prod_Trade[Prod_Trade.year == 2012].groupby(by = 'month').aggregate({'Sales':np.sum})

# 绘制销售额趋势图

Month_Sales.plot(title = '2012年各月销售趋势', ax = ax2, legend = False)

# 删除x轴标签

ax2.set_xlabel('')

# 设置第三个子图的布局

ax3 = plt.subplot2grid(shape = (2,3), loc = (0,2), rowspan = 2)

# 绘制各运输方式的成本箱线图

sns.boxplot(x = 'Transport', y = 'Trans_Cost', data = Prod_Trade, ax = ax3)

# 添加标题

ax3.set_title('各运输方式成本分布')

# 删除x轴标签

ax3.set_xlabel('')

# 修改y轴标签

ax3.set_ylabel('运输成本')

# 设置第四个子图的布局

ax4 = plt.subplot2grid(shape = (2,3), loc = (1,0), colspan = 2)

# 2012年客单价分布直方图

sns.distplot(Prod_Trade.Sales[Prod_Trade.year == 2012], bins = 40, norm_hist = True, ax = ax4, hist_kws = {'color':'steelblue'}, kde_kws=({'linestyle':'--', 'color':'red'}))

# 添加标题

ax4.set_title('2012年客单价分布图')

# 修改x轴标签

ax4.set_xlabel('销售额')

# 调整子图之间的水平间距和高度间距

plt.subplots_adjust(hspace=0.6, wspace=0.3)

# 图形显示

plt.show()

359f61255fd308d82cdcedd1cb2af8dab19.png

406fc8d1767b717113a4220e4c9fc2a596d.png

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值