博客
关于我
Python-可视化
阅读量:96 次
发布时间:2019-02-26

本文共 17513 字,大约阅读时间需要 58 分钟。

文章目录

饼图

# %%# 饼图的绘制# 导入第三方模块import matplotlib.pyplot as plt# 构造数据edu = [0.2515, 0.3724, 0.3336, 0.0368, 0.0057]labels = ['中专', '大专', '本科', '硕士', '其他']plt.rcParams['font.sans-serif'] = ['SimHei']plt.rcParams['axes.unicode_minus'] = False# 绘制饼图plt.pie(x=edu,  # 绘图数据        labels=labels,  # 添加教育水平标签        autopct='%.1f%%'  # 设置百分比的格式,这里保留一位小数        )# 添加图标题plt.title('失信用户的教育水平分布')# 显示图形plt.show()# %%# 添加修饰的饼图explode = [0, 0.1, 0, 0, 0]  # 生成数据,用于突出显示大专学历人群# 自定义颜色,英文单词也可以colors = ['#9999ff', '#ff9999', '#7777aa', '#2442aa', '#dd5555']  # 中文乱码和坐标轴负号的处理plt.rcParams['font.sans-serif'] = ['SimHei']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()# %%# 导入第三方模块import pandas as pd# 构建序列data1 = pd.Series({   '中专': 0.2515, '大专': 0.3724, '本科': 0.3336, '硕士': 0.0368, '其他': 0.0057})# 将序列的名称设置为空字符,否则绘制的饼图左边会出现None这样的字眼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()

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

垂直条形图

# %%# 条形图的绘制--垂直条形图# 读入数据GDP = pd.read_excel(    r'E:\PyCharmProject\venv\StataAnalysis_Files\从零开始学Python--数据分析与挖掘\第6章 Python数据可视化\Province GDP 2017.xlsx')# 设置绘图风格(不妨使用R语言中的ggplot2风格)plt.style.use('ggplot')# 绘制条形图plt.bar(x=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()

在这里插入图片描述

水平条形图

# %%# 条形图的绘制--水平条形图# 对读入的数据作升序排序GDP.sort_values(by='GDP', inplace=True)# 绘制条形图plt.barh(y=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()

在这里插入图片描述

堆叠条形图

# %%# 条形图的绘制--堆叠条形图# 读入数据Industry_GDP = pd.read_excel(    r'E:\PyCharmProject\venv\StataAnalysis_Files\从零开始学Python--数据分析与挖掘\第6章 Python数据可视化\Industry_GDP.xlsx')# 取出四个不同的季度标签,用作堆叠条形图x轴的刻度标签Quarters = Industry_GDP.Quarter.unique()# 取出第一产业的四季度值Industry1 = Industry_GDP.GPD[Industry_GDP.Industry_Type == '第一产业']# 重新设置行索引Industry1.index = range(len(Quarters))# 取出第二产业的四季度值Industry2 = Industry_GDP.GPD[Industry_GDP.Industry_Type == '第二产业']# 重新设置行索引Industry2.index = range(len(Quarters))# 取出第三产业的四季度值Industry3 = Industry_GDP.GPD[Industry_GDP.Industry_Type == '第三产业']# 绘制堆叠条形图# 各季度下第一产业的条形图plt.bar(x=range(len(Quarters)), height=Industry1, color='steelblue', label='第一产业', tick_label=Quarters)# 各季度下第二产业的条形图plt.bar(x=range(len(Quarters)), height=Industry2, bottom=Industry1, color='green', label='第二产业')# 各季度下第三产业的条形图plt.bar(x=range(len(Quarters)), height=Industry3, bottom=Industry1 + Industry2, color='red', label='第三产业')# 添加y轴标签plt.ylabel('生成总值(亿)')# 添加图形标题plt.title('2017年各季度三产业总值')# 显示各产业的图例plt.legend()# 显示图形plt.show()

在这里插入图片描述

水平交错条形图

# %%# 条形图的绘制--水平交错条形图# 导入第三方模块import numpy as np# 读入数据HuRun = pd.read_excel(    r'E:\PyCharmProject\venv\StataAnalysis_Files\从零开始学Python--数据分析与挖掘\第5章 Python数据处理工具--Pandas\HuRun.xlsx')# 取出城市名称Cities = HuRun.City.unique()# 取出2016年各城市亿万资产家庭数Counts2016 = HuRun.Counts[HuRun.Year == 2016]# 取出2017年各城市亿万资产家庭数Counts2017 = HuRun.Counts[HuRun.Year == 2017]# 绘制水平交错条形图bar_width = 0.4plt.bar(x=np.arange(len(Cities)), height=Counts2016, label='2016', color='steelblue', width=bar_width)plt.bar(x=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模块之垂直或水平条形图

# %%# 绘图(此时的数据集在前文已经按各省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()

在这里插入图片描述

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 snssns.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()

在这里插入图片描述

pandas模块之水平交错条形图

# %%import pandas as pd# 读入数据Titanic = pd.read_csv(    r'E:\PyCharmProject\venv\StataAnalysis_Files\从零开始学Python--数据分析与挖掘\第6章 Python数据可视化\titanic_train.csv')# 绘制水平交错条形图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()

在这里插入图片描述

matplotlib模块之直方图

# %%# 检查年龄是否有缺失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()

在这里插入图片描述

pandas模块之直方图和核密度图

# %%# 绘制直方图Titanic.Age.plot(kind='hist', bins=20, color='steelblue', edgecolor='black', density=True, label='直方图')# 绘制核密度图Titanic.Age.plot(kind='kde', color='red', label='核密度图')# 添加x轴和y轴标签plt.xlabel('年龄')plt.ylabel('核密度值')# 添加标题plt.title('乘客年龄分布')# 显示图例plt.legend()# 显示图形plt.show()

在这里插入图片描述

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()# 绘制男女乘客年龄的核密度图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()

在这里插入图片描述

在这里插入图片描述

单个箱线图

# %%# 读取数据Sec_Buildings = pd.read_excel(    r'E:\PyCharmProject\venv\StataAnalysis_Files\从零开始学Python--数据分析与挖掘\第6章 Python数据可视化\sec_buildings.xlsx')# 绘制箱线图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()

在这里插入图片描述

分组箱线图

# %%# 二手房在各行政区域的平均单价group_region = Sec_Buildings.groupby('region')avg_price = group_region.aggregate({   'price_unit': np.mean}).sort_values('price_unit', ascending=False)# 通过循环,将不同行政区域的二手房存储到列表中region_price = []for region in avg_price.index:    region_price.append(Sec_Buildings.price_unit[Sec_Buildings.region == region])# 绘制分组箱线图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()# %%# 绘制分组箱线图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()

在这里插入图片描述

在这里插入图片描述

小提琴图

# %%# 读取数据tips = pd.read_csv(r'E:\PyCharmProject\venv\StataAnalysis_Files\从零开始学Python--数据分析与挖掘\第6章 Python数据可视化\tips.csv')# 绘制分组小提琴图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()

在这里插入图片描述

单条折线图

# %%# 数据读取wechat = pd.read_excel(r'E:\PyCharmProject\venv\StataAnalysis_Files\从零开始学Python--数据分析与挖掘\第6章 Python数据可视化\wechat.xlsx')# 绘制单条折线图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()

在这里插入图片描述

多条折线图

# %%# 导入模块,用于日期刻度的修改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='阅读人次'         )# 获取图的坐标信息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'E:\PyCharmProject\venv\StataAnalysis_Files\从零开始学Python--数据分析与挖掘\第6章 Python数据可视化\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()

在这里插入图片描述

在这里插入图片描述

pandas模块之单组散点图

# %%# 读入数据iris = pd.read_csv(r'E:\PyCharmProject\venv\StataAnalysis_Files\从零开始学Python--数据分析与挖掘\第6章 Python数据可视化\iris.csv')# 绘制散点图plt.scatter(x=iris.Petal_Width,  # 指定散点图的x轴数据            y=iris.Petal_Length,  # 指定散点图的y轴数据            color='steelblue'  # 指定散点图中点的颜色            )# 添加x轴和y轴标签plt.xlabel('花瓣宽度')plt.ylabel('花瓣长度')# 添加标题plt.title('鸢尾花的花瓣宽度与长度关系')# 显示图形plt.show()# %%# 绘制散点图iris.plot(x='Petal_Width', y='Petal_Length', kind='scatter', title='鸢尾花的花瓣宽度与长度关系')# 修改x轴和y轴标签plt.xlabel('花瓣宽度')plt.ylabel('花瓣长度')# 显示图形plt.show()

在这里插入图片描述

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()

在这里插入图片描述

气泡图

# %%# 读取数据Prod_Category = pd.read_excel(    r'E:\PyCharmProject\venv\StataAnalysis_Files\从零开始学Python--数据分析与挖掘\第6章 Python数据可视化\SuperMarket.xlsx')# 将利润率标准化到[0,1]之间(因为利润率中有负数),然后加上微小的数值0.001range_diff = Prod_Category.Profit_Ratio.max() - Prod_Category.Profit_Ratio.min()Prod_Category['std_ratio'] = (Prod_Category.Profit_Ratio - Prod_Category.Profit_Ratio.min()) / range_diff + 0.001# 绘制办公用品的气泡图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()

在这里插入图片描述

热力图

# %%# 读取数据Sales = pd.read_excel(r'E:\PyCharmProject\venv\StataAnalysis_Files\从零开始学Python--数据分析与挖掘\第6章 Python数据可视化\Sales.xlsx')# 根据交易日期,衍生出年份和月份字段Sales['year'] = Sales.Date.dt.yearSales['month'] = Sales.Date.dt.month# 统计每年各月份的销售总额Summary = Sales.pivot_table(index='month', columns='year', values='Sales', aggfunc=np.sum)# 绘制热力图sns.heatmap(data=Summary,  # 指定绘图数据            cmap='PuBuGn',  # 指定填充色            linewidths=.1,  # 设置每个单元格边框的宽度            annot=True,  # 显示数值            fmt='.1e'  # 以科学计算法显示数据            )# 添加标题plt.title('每年各月份销售总额热力图')# 显示图形plt.show()# %%# 读取数据Prod_Trade = pd.read_excel(    r'E:\PyCharmProject\venv\StataAnalysis_Files\从零开始学Python--数据分析与挖掘\第6章 Python数据可视化\Prod_Trade.xlsx')# 衍生出交易年份和月份字段Prod_Trade['year'] = Prod_Trade.Date.dt.yearProd_Trade['month'] = Prod_Trade.Date.dt.month# 设置大图框的长和高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()

在这里插入图片描述

在这里插入图片描述

词云分词

import jiebaimport wordcloudtext = open(r'text.txt',encoding='utf-8').read()def split_words(text):    cut_text = jieba.cut(text)    string = ','.join(cut_text)    stop_words = ['我们', '你们',]    word_cloud = wordcloud.WordCloud(        font_path=r'.\simhei.ttf',        background_color='white',        width=500,        height=350,        max_font_size=100,        min_font_size=10,        stopwords=stop_words,        scale=15,    )    word_cloud.generate(string)    word_cloud.to_file(r'词云分词.png')split_words(text=text)

在这里插入图片描述

转载地址:http://mhwu.baihongyu.com/

你可能感兴趣的文章
Manjaro 24.1 “Xahea” 发布!具有 KDE Plasma 6.1.5、GNOME 46 和最新的内核增强功能
查看>>
mapping文件目录生成修改
查看>>
MapReduce程序依赖的jar包
查看>>
mariadb multi-source replication(mariadb多主复制)
查看>>
MariaDB的简单使用
查看>>
MaterialForm对tab页进行隐藏
查看>>
Member var and Static var.
查看>>
memcached高速缓存学习笔记001---memcached介绍和安装以及基本使用
查看>>
memcached高速缓存学习笔记003---利用JAVA程序操作memcached crud操作
查看>>
Memcached:Node.js 高性能缓存解决方案
查看>>
memcache、redis原理对比
查看>>
memset初始化高维数组为-1/0
查看>>
Metasploit CGI网关接口渗透测试实战
查看>>
Metasploit Web服务器渗透测试实战
查看>>
MFC模态对话框和非模态对话框
查看>>
Moment.js常见用法总结
查看>>
MongoDB出现Error parsing command line: unrecognised option ‘--fork‘ 的解决方法
查看>>
mxGraph改变图形大小重置overlay位置
查看>>
MongoDB可视化客户端管理工具之NoSQLbooster4mongo
查看>>
Mongodb学习总结(1)——常用NoSql数据库比较
查看>>