当前位置: 首页 > news >正文

泉州做网站的公司最佳磁力搜索天堂

泉州做网站的公司,最佳磁力搜索天堂,苏州做网站设计的公司,微信端网站开发比较#xff08;一#xff09;利用python绘制条形图 条形图#xff08;Barplot#xff09;简介 条形图主要用来比较不同类别间的数据差异#xff0c;一条轴表示类别#xff0c;另一条则表示对应的数值度量。 快速绘制 基于seaborn import seaborn as sns import matplo…比较一利用python绘制条形图 条形图Barplot简介 条形图主要用来比较不同类别间的数据差异一条轴表示类别另一条则表示对应的数值度量。 快速绘制 基于seaborn import seaborn as sns import matplotlib.pyplot as plt# 导入数据 tips sns.load_dataset(tips)# 利用barplot函数快速绘制 sns.barplot(xtotal_bill, yday, datatips, estimatorsum, errorbarNone, color#69b3a2)plt.show()基于matplotlib import matplotlib.pyplot as plt# 导入数据 tips sns.load_dataset(tips) grouped_tips tips.groupby(day)[total_bill].sum().reset_index()# 利用bar函数快速绘制 plt.bar(grouped_tips.day, grouped_tips.total_bill)plt.show()基于pandas import matplotlib.pyplot as plt import pandas as pd# 导入数据 tips sns.load_dataset(tips) grouped_tips tips.groupby(day)[total_bill].sum().reset_index()# 利用plot.bar函数快速绘制 grouped_tips.plot.bar(xday, ytotal_bill, rot0)plt.show()定制多样化的条形图 自定义条形图一般是结合使用场景对相关参数进行修改并辅以其他的绘图知识。参数信息可以通过官网进行查看其他的绘图知识则更多来源于实战经验大家不妨将接下来的绘图作为一种学习经验以便于日后总结。 通过seaborn绘制多样化的条形图 seaborn主要利用barplot绘制条形图可以通过seaborn.barplot了解更多用法 修改参数 import seaborn as sns import matplotlib.pyplot as plt import numpy as npsns.set(fontSimHei, font_scale0.8, styledarkgrid) # 解决Seaborn中文显示问题# 导入数据 tips sns.load_dataset(tips)# 构造子图 fig, ax plt.subplots(2,2,constrained_layoutTrue, figsize(8, 8))# 修改方向-垂直 ax_sub sns.barplot(ytotal_bill, xday, datatips, estimatorsum, errorbarNone, color#69b3a2,axax[0][0]) ax_sub.set_title(垂直条形图)# 自定义排序 ax_sub sns.barplot(ytotal_bill, xday, datatips, estimatorsum, errorbarNone, color#69b3a2,order[Fri,Thur,Sat,Sun],axax[0][1]) ax_sub.set_title(自定义排序)# 数值排序 df tips.groupby(day)[total_bill].sum().sort_values(ascendingFalse).reset_index() ax_sub sns.barplot(yday, xtotal_bill, datadf, errorbarNone, color#69b3a2,orderdf[day],axax[1][0]) ax_sub.set_title(数值排序)# 添加误差线 ax_sub sns.barplot(xday, ytotal_bill, datatips, estimatornp.mean, errorbar(ci, 85), capsize.2, colorlightblue,axax[1][1]) ax_sub.set_title(添加误差线)plt.show()分组条形图 import seaborn as sns import matplotlib.pyplot as plt import numpy as npsns.set(styledarkgrid)# 导入数据 tips sns.load_dataset(tips)fig, ax plt.subplots(figsize(4, 4))# 分组条形图 colors [#69b3a2, #4374B3] sns.barplot(xday, ytotal_bill, huesmoker, datatips, errorbarNone, palettecolors)plt.show()# 分组/子分组条形图 sns.catplot(xsex, ytotal_bill, huesmoker, colday, datatips, kindbar, height4, aspect.7)plt.show()引申-数量堆积条形图 import seaborn as sns import numpy as np import matplotlib.pyplot as plt import matplotlib.patches as mpatchessns.set(styledarkgrid)# 导入数据 tips sns.load_dataset(tips) df tips.groupby([day, smoker])[total_bill].sum().reset_index() smoker_df df[df[smoker]Yes] non_smoker_df df[df[smoker]No]# 布局 plt.figure(figsize(6, 4))# 非吸烟者的条形图 bar1 sns.barplot(xday, ytotal_bill, datanon_smoker_df, colorlightblue) # 吸烟者的条形图底部开始位置设置为非吸烟者的total_bill值即吸烟者条形图在上面 bar2 sns.barplot(xday, ytotal_bill, bottomnon_smoker_df[total_bill], datasmoker_df, colordarkblue)# 图例 top_bar mpatches.Patch(colordarkblue, labelsmoker Yes) bottom_bar mpatches.Patch(colorlightblue, labelsmoker No) plt.legend(handles[top_bar, bottom_bar])plt.show()引申-百分比堆积条形图 import seaborn as sns import matplotlib.pyplot as plt import pandas as pd# 导入数据 tips sns.load_dataset(tips)# 计算百分比 day_total_bill tips.groupby(day)[total_bill].sum() # 每日数据 group_total_bill tips.groupby([day, smoker])[total_bill].sum().reset_index() # 每日每组数据 group_total_bill[percent] group_total_bill.apply(lambda row: row[total_bill] / day_total_bill[row[day]] * 100, axis1)# 将数据分成smoker和non-smoker两份方便我们绘制两个条形图 smoker_df group_total_bill[group_total_bill[smoker] Yes] non_smoker_df group_total_bill[group_total_bill[smoker] No]# 布局 plt.figure(figsize(6, 4))# 非吸烟者的条形图 bar1 sns.barplot(xday, ypercent, datanon_smoker_df, colorlightblue) # 吸烟者的条形图底部开始位置设置为非吸烟者的total_bill值即吸烟者条形图在上面 bar2 sns.barplot(xday, ypercent, bottomnon_smoker_df[percent], datasmoker_df, colordarkblue)# 图例 top_bar mpatches.Patch(colordarkblue, labelsmoker Yes) bottom_bar mpatches.Patch(colorlightblue, labelsmoker No) plt.legend(handles[top_bar, bottom_bar])plt.show()通过seaborn绘制多样化的条形图 seaborn主要利用barh绘制条形图可以通过matplotlib.pyplot.barh了解更多用法 修改参数 import matplotlib as mpl import matplotlib.pyplot as plt import numpy as np import pandas as pdmpl.rcParams.update(mpl.rcParamsDefault) # 恢复默认的matplotlib样式 plt.rcParams[font.sans-serif] [SimHei] # 用来正常显示中文标签# 自定义数据 height [3, 12, 5, 18, 45] bars (A, B, C, D, E) y_pos np.arange(len(bars)) x_pos np.arange(len(bars))# 初始化布局 fig plt.figure(figsize(8,8))# 水平方向-水平条形图 plt.subplot(3, 3, 1) plt.barh(y_pos, height) plt.yticks(y_pos, bars) plt.title(水平条形图)# 指定顺序 height_order, bars_order zip(*sorted(zip(height, bars), reverseFalse)) # 自定义顺序plt.subplot(3, 3, 2) plt.barh(y_pos, height_order) plt.yticks(y_pos, bars_order) plt.title(指定顺序)# 自定义颜色 plt.subplot(3, 3, 3) plt.bar(x_pos, height, color[black, red, green, blue, cyan]) plt.xticks(x_pos, bars) plt.title(自定义颜色)# 自定义颜色-边框颜色 plt.subplot(3, 3, 4) plt.bar(x_pos, height, color(0.1, 0.1, 0.1, 0.1), edgecolorblue) plt.xticks(x_pos, bars) plt.title(自定义边框颜色)# 控制距离 width [0.1,0.2,3,1.5,0.3] x_pos_width [0,0.3,2,4.5,5.5]plt.subplot(3, 3, 5) plt.bar(x_pos_width, height, widthwidth) plt.xticks(x_pos_width, bars) plt.title(控制距离)# 控制宽度 x_pos_space [0,1,5,8,9]plt.subplot(3, 3, 6) plt.bar(x_pos_space, height) plt.xticks(x_pos_space, bars) plt.title(控制宽度)# 自定义布局 plt.subplot(3, 3, 7) plt.bar(x_pos, height) plt.xticks(x_pos, bars, colororange, rotation90) # 自定义x刻度名称颜色自定义旋转 plt.xlabel(category, fontweightbold, color orange, fontsize18) # 自定义x标签 plt.yticks(colororange) # 自定义y刻度名称颜色plt.title(自定义布局)# 添加误差线 err [val * 0.1 for val in height] # 计算误差这里假设误差为height的10%plt.subplot(3, 3, 8) plt.bar(x_pos, height, yerrerr, alpha0.5, ecolorblack, capsize10) plt.xticks(x_pos, bars) plt.title(添加误差线)# 增加数值文本信息 plt.subplot(3, 3, 9) ax plt.bar(x_pos, height) for bar in ax:yval bar.get_height()plt.text(bar.get_x() bar.get_width()/2.0, yval, int(yval), vabottom) # va参数代表垂直对齐方式 plt.xticks(x_pos, bars) plt.title(增加数值文本信息)fig.tight_layout() # 自动调整间距 plt.show()分组条形图 import numpy as np import matplotlib.pyplot as plt# 宽度设置 barWidth 0.25# 自定义数据 bars1 [12, 30, 1, 8, 22] bars2 [28, 6, 16, 5, 10] bars3 [29, 3, 24, 25, 17]# x位置 r1 np.arange(len(bars1)) r2 [x barWidth for x in r1] r3 [x barWidth for x in r2]# 绘制分组条形图 plt.bar(r1, bars1, color#7f6d5f, widthbarWidth, edgecolorwhite, labelg1) plt.bar(r2, bars2, color#557f2d, widthbarWidth, edgecolorwhite, labelg2) plt.bar(r3, bars3, color#2d7f5e, widthbarWidth, edgecolorwhite, labelg3)# 轴标签、图例 plt.xlabel(group, fontweightbold) plt.xticks([r barWidth for r in range(len(bars1))], [A, B, C, D, E]) plt.legend()plt.show()数量堆积条形图 import numpy as np import matplotlib.pyplot as plt import pandas as pd# 自定义数据 bars1 [12, 28, 1, 8, 22] bars2 [28, 7, 16, 4, 10] bars3 [25, 3, 23, 25, 17]# bars1 bars2的高度 bars np.add(bars1, bars2).tolist()# x位置 r [0,1,2,3,4]# bar名称、宽度 names [A,B,C,D,E] barWidth 1# 底部bar plt.bar(r, bars1, color#7f6d5f, edgecolorwhite, widthbarWidth, labelg1) # 中间bar plt.bar(r, bars2, bottombars1, color#557f2d, edgecolorwhite, widthbarWidth, labelg2) # 顶部bar plt.bar(r, bars3, bottombars, color#2d7f5e, edgecolorwhite, widthbarWidth, labelg3)# x轴设置、图例 plt.xticks(r, names, fontweightbold) plt.xlabel(group) plt.legend()plt.show()百分比堆积条形图 import numpy as np import matplotlib.pyplot as plt import pandas as pd# 自定义数据 r [0,1,2,3,4] # x位置 raw_data {greenBars: [20, 1.5, 7, 10, 5], orangeBars: [5, 15, 5, 10, 15],blueBars: [2, 15, 18, 5, 10]} df pd.DataFrame(raw_data)# 转为百分比 totals [ijk for i,j,k in zip(df[greenBars], df[orangeBars], df[blueBars])] greenBars [i / j * 100 for i,j in zip(df[greenBars], totals)] orangeBars [i / j * 100 for i,j in zip(df[orangeBars], totals)] blueBars [i / j * 100 for i,j in zip(df[blueBars], totals)]# bar名称、宽度 barWidth 0.85 names (A,B,C,D,E)# 底部bar plt.bar(r, greenBars, color#b5ffb9, edgecolorwhite, widthbarWidth, labelg1) # 中间bar plt.bar(r, orangeBars, bottomgreenBars, color#f9bc86, edgecolorwhite, widthbarWidth, labelg2) # 顶部bar plt.bar(r, blueBars, bottom[ij for i,j in zip(greenBars, orangeBars)], color#a3acff, edgecolorwhite, widthbarWidth, labelg3)# x轴、图例 plt.xticks(r, names) plt.xlabel(group) plt.legend()plt.show()通过pandas绘制多样化的条形图 pandas主要利用barh绘制条形图可以通过pandas.DataFrame.plot.barh了解更多用法 修改参数 import matplotlib as mpl import matplotlib.pyplot as plt import numpy as np import pandas as pdplt.rcParams[font.sans-serif] [SimHei] # 用来正常显示中文标签# 自定义数据 category [Group1]*30 [Group2]*50 [Group3]*20 df pd.DataFrame({category: category}) values df[category].value_counts()# 初始化布局 fig plt.figure(figsize(8,4))# 水平方向-水平条形图 plt.subplot(1, 2, 1) values.plot.barh(gridTrue) plt.title(水平条形图)# 自定义顺序、颜色 # 指定顺序 desired_order [Group1, Group2, Group3] values_order values.reindex(desired_order) # 指定颜色 colors [#69b3a2, #cb1dd1, palegreen]plt.subplot(1, 2, 2) values.plot.bar(colorcolors,gridTrue, ) plt.title(自定义顺序、颜色)fig.tight_layout() # 自动调整间距 plt.show()分组条形图 import pandas as pd import matplotlib.pyplot as plt# 自定义数据 data {Product: [Product A, Product A, Product A, Product B, Product B, Product B],Segment: [Segment 1, Segment 2, Segment 3, Segment 1, Segment 2, Segment 3],Amount_sold: [100, 120, 120, 80, 160, 150] }df pd.DataFrame(data) pivot_df df.pivot(indexSegment,columnsProduct,valuesAmount_sold)# 分组条形图 pivot_df.plot.bar(gridTrue)plt.show()数量堆积条形图 import pandas as pd import matplotlib.pyplot as plt# 自定义数据 data {Product: [Product A, Product A, Product A, Product B, Product B, Product B],Segment: [Segment 1, Segment 2, Segment 3, Segment 1, Segment 2, Segment 3],Amount_sold: [100, 120, 120, 80, 160, 150] }df pd.DataFrame(data) pivot_df df.pivot(indexSegment,columnsProduct,valuesAmount_sold)# 堆积条形图 pivot_df.plot.bar(stackedTrue,gridTrue)plt.show()百分比堆积条形图 import pandas as pd import matplotlib.pyplot as plt# 自定义数据 data {Product: [Product A, Product A, Product A, Product B, Product B, Product B],Segment: [Segment 1, Segment 2, Segment 3, Segment 1, Segment 2, Segment 3],Amount_sold: [100, 120, 120, 80, 160, 150] }df pd.DataFrame(data) pivot_df df.pivot(indexSegment,columnsProduct,valuesAmount_sold) pivot_df_percentage pivot_df.div(pivot_df.sum(axis1), axis0) * 100# 百分比堆积条形图 pivot_df_percentage.plot.bar(stackedTrue,gridTrue)# 图例 plt.legend(bbox_to_anchor(1.04, 1),locupper left) plt.show()总结 以上通过seaborn的barplot、matplotlib的bar和pandas的bar快速绘制条形图并通过修改参数或者辅以其他绘图知识自定义各种各样的条形图来适应相关使用场景。 共勉
http://www.dnsts.com.cn/news/3072.html

相关文章:

  • 做网站要什么知识搜索引擎推广有哪些
  • 怎么做软件推广赚钱seo优化网站快速排名
  • 合肥做网站建设公司福建seo学校
  • 山西做二级建筑资料在哪个网站成人职业培训学校
  • 邦派巴洛特网站是谁做的呀seo网站结构优化
  • 网页版微信可以传文件吗湖北seo服务
  • 推广类软文seo推广主要做什么
  • ps做网站动图建站教程
  • 网站目录结构设计网络营销与传统营销有哪些区别
  • 帮助中心网站源码条友网
  • 网站制作培训一般要多少钱网站主题
  • 优质的常州网站建设吸引人的软文标题例子
  • 网站建设和架构线上渠道推广有哪些方式
  • 跨境独立站建站企业官网定制设计
  • 领域网站建设营销网站建设规划
  • 动态网站设计分析百度推广联系方式
  • 网站开发课程百度云湖州网站建设制作
  • 免费发布信息的平台有哪些微信公众号seo
  • 企业公示信息查询系统 江苏seo是什么职业做什么的
  • 商城网站开发技术有哪些中国最新军事新闻
  • 线上渠道推广有哪些方式张北网站seo
  • 特定ip段访问网站代码沈阳百度推广排名优化
  • 外贸网站域名能用cn做后缀吗广东培训seo
  • 漯河装修公司网站建设真正永久免费的建站系统有哪些
  • 小程序免费制作平台登录石家庄网络推广优化
  • 比特币做空网站品牌策划方案怎么做
  • 海南城乡建设庁网站上海短视频seo优化网站
  • 青岛昌隆文具网站是哪家公司做的平台优化是指什么
  • 网站建设实现用户登录武汉网络推广公司排名
  • 吴中区网站设计公司磁力搜索器在线