熬夜整理的资料:分享Python数据可视化图表代码和案例给大家
程序员文章站
2022-03-26 20:08:58
前言 本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。 闲话不多说,直接上干货 1华夫饼图 waffle可以使用该pywaffle软件包创建该图表,并用于显示较大人群中各组的组成。 #! pip install pywaffle ......
前言
本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。
闲话不多说,直接上干货
1华夫饼图
waffle可以使用该pywaffle软件包创建该图表,并用于显示较大人群中各组的组成。
#! pip install pywaffle # reference: https://*.com/questions/41400136/how-to-do-waffle-charts-in-python-square-piechart from pywaffle import waffle # import df_raw = pd.read_csv("data/mpg_ggplot2.csv") # prepare data df = df_raw.groupby('class').size().reset_index(name='counts') n_categories = df.shape[0] colors = [plt.cm.inferno_r(i/float(n_categories)) for i in range(n_categories)] # draw plot and decorate fig = plt.figure( figureclass=waffle, plots={ '111': { 'values': df['counts'], 'labels': ["{0} ({1})".format(n[0], n[1]) for n in df[['class', 'counts']].itertuples()], 'legend': {'loc': 'upper left', 'bbox_to_anchor': (1.05, 1), 'fontsize': 12}, 'title': {'label': '# vehicles by class', 'loc': 'center', 'fontsize':18} }, }, rows=7, colors=colors, figsize=(16, 9) )
#! pip install pywaffle from pywaffle import waffle # import # df_raw = pd.read_csv("data/mpg_ggplot2.csv") # prepare data # by class data df_class = df_raw.groupby('class').size().reset_index(name='counts_class') n_categories = df_class.shape[0] colors_class = [plt.cm.set3(i/float(n_categories)) for i in range(n_categories)] # by cylinders data df_cyl = df_raw.groupby('cyl').size().reset_index(name='counts_cyl') n_categories = df_cyl.shape[0] colors_cyl = [plt.cm.spectral(i/float(n_categories)) for i in range(n_categories)] # by make data df_make = df_raw.groupby('manufacturer').size().reset_index(name='counts_make') n_categories = df_make.shape[0] colors_make = [plt.cm.tab20b(i/float(n_categories)) for i in range(n_categories)] # draw plot and decorate fig = plt.figure( figureclass=waffle, plots={ '311': { 'values': df_class['counts_class'], 'labels': ["{1}".format(n[0], n[1]) for n in df_class[['class', 'counts_class']].itertuples()], 'legend': {'loc': 'upper left', 'bbox_to_anchor': (1.05, 1), 'fontsize': 12, 'title':'class'}, 'title': {'label': '# vehicles by class', 'loc': 'center', 'fontsize':18}, 'colors': colors_class }, '312': { 'values': df_cyl['counts_cyl'], 'labels': ["{1}".format(n[0], n[1]) for n in df_cyl[['cyl', 'counts_cyl']].itertuples()], 'legend': {'loc': 'upper left', 'bbox_to_anchor': (1.05, 1), 'fontsize': 12, 'title':'cyl'}, 'title': {'label': '# vehicles by cyl', 'loc': 'center', 'fontsize':18}, 'colors': colors_cyl }, '313': { 'values': df_make['counts_make'], 'labels': ["{1}".format(n[0], n[1]) for n in df_make[['manufacturer', 'counts_make']].itertuples()], 'legend': {'loc': 'upper left', 'bbox_to_anchor': (1.05, 1), 'fontsize': 12, 'title':'manufacturer'}, 'title': {'label': '# vehicles by make', 'loc': 'center', 'fontsize':18}, 'colors': colors_make } }, rows=9, figsize=(16, 14) )
2 饼图
饼图是显示组组成的经典方法。但是,如今一般不建议使用它,因为馅饼部分的面积有时可能会引起误解。因此,如果要使用饼图,强烈建议明确写下饼图各部分的百分比或数字。
# import df_raw = pd.read_csv("data/mpg_ggplot2.csv") # prepare data df = df_raw.groupby('class').size() # make the plot with pandas df.plot(kind='pie', subplots=true, figsize=(8, 8), dpi= 80) plt.title("pie chart of vehicle class - bad") plt.ylabel("") plt.show()
# import df_raw = pd.read_csv("data/mpg_ggplot2.csv") # prepare data df = df_raw.groupby('class').size().reset_index(name='counts') # draw plot fig, ax = plt.subplots(figsize=(12, 7), subplot_kw=dict(aspect="equal"), dpi= 80) data = df['counts'] categories = df['class'] explode = [0,0,0,0,0,0.1,0] def func(pct, allvals): absolute = int(pct/100.*np.sum(allvals)) return "{:.1f}% ({:d} )".format(pct, absolute) wedges, texts, autotexts = ax.pie(data, autopct=lambda pct: func(pct, data), textprops=dict(color="w"), colors=plt.cm.dark2.colors, startangle=140, explode=explode) # decoration ax.legend(wedges, categories, title="vehicle class", loc="center left", bbox_to_anchor=(1, 0, 0.5, 1)) plt.setp(autotexts, size=10, weight=700) ax.set_title("class of vehicles: pie chart") plt.show()
3 树状图
树形图类似于饼形图,并且可以更好地完成工作,而不会误导每个组的贡献。
# pip install squarify import squarify # import data df_raw = pd.read_csv("data/mpg_ggplot2.csv") # prepare data df = df_raw.groupby('class').size().reset_index(name='counts') labels = df.apply(lambda x: str(x[0]) + "\n (" + str(x[1]) + ")", axis=1) sizes = df['counts'].values.tolist() colors = [plt.cm.spectral(i/float(len(labels))) for i in range(len(labels))] # draw plot plt.figure(figsize=(12,8), dpi= 80) squarify.plot(sizes=sizes, label=labels, color=colors, alpha=.8) # decorate plt.title('treemap of vechile class') plt.axis('off') plt.show()
4 条形图
条形图是一种基于计数或任何给定指标可视化项目的经典方法。在下面的图表中,我为每个项目使用了不同的颜色,但是您通常可能希望为所有项目选择一种颜色,除非您按组对它们进行着色。颜色名称存储在all_colors下面的代码中。您可以通过在中设置color参数来更改条形的颜色。
import random # import data df_raw = pd.read_csv("data/mpg_ggplot2.csv") # prepare data df = df_raw.groupby('manufacturer').size().reset_index(name='counts') n = df['manufacturer'].unique().__len__()+1 all_colors = list(plt.cm.colors.cnames.keys()) random.seed(100) c = random.choices(all_colors, k=n) # plot bars plt.figure(figsize=(16,10), dpi= 80) plt.bar(df['manufacturer'], df['counts'], color=c, width=.5) for i, val in enumerate(df['counts'].values): plt.text(i, val, float(val), horizontalalignment='center', verticalalignment='bottom', fontdict={'fontweight':500, 'size':12}) # decoration plt.gca().set_xticklabels(df['manufacturer'], rotation=60, horizontalalignment= 'right') plt.title("number of vehicles by manaufacturers", fontsize=22) plt.ylabel('# vehicles') plt.ylim(0, 45) plt.show()
不管你是零基础还是有基础都可以获取到自己相对应的学习礼包!包括python软件工具和2020最新入门到实战教程。加群695185429即可免费获取。