分组条形图 pandas

编程入门 行业动态 更新时间:2024-10-26 23:34:06
本文介绍了分组条形图 pandas 的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在名为df的熊猫DataFrame中有一张桌子:

I have a table in a pandas DataFrame named df:

+--- -----+------------+-------------+----------+------------+-----------+ |avg_views| avg_orders | max_views |max_orders| min_views |min_orders | +---------+------------+-------------+----------+------------+-----------+ | 23 | 123 | 135 | 500 | 3 | 1 | +---------+------------+-------------+----------+------------+-----------+

我现在正在寻找的是绘制一个分组的条形图,该条形图向我显示 一张条形图中的视图和订单的平均(平均,最大,最小).

What I am looking for now is to plot a grouped bar graph which shows me (avg, max, min) of views and orders in one single bar chart.

即在x轴上会存在以一定距离分开的视图和订单 和3条(平均,最大值,最小值)的视图和类似的订单.

i.e on x axis there would be Views and orders separated by a distance and 3 bars of (avg, max, min) for views and similarly for orders.

我已经附上了一个示例条形图图像,只是想知道条形图的外观.

I have attached a sample bar graph image, just to know how the bar graph should look.

绿色表示平均值,黄色表示最大值,粉红色表示平均值.

Green color should be for avg, yellow for max and pink for avg.

我从在matplotlib ,但对我不起作用:

I took the following code from setting spacing between grouped bar plots in matplotlib but it is not working for me:

plt.figure(figsize=(13, 7), dpi=300) groups = [[23, 135, 3], [123, 500, 1]] group_labels = ['views', 'orders'] num_items = len(group_labels) ind = np.arange(num_items) margin = 0.05 width = (1. - 2. * margin) / num_items s = plt.subplot(1, 1, 1) for num, vals in enumerate(groups): print 'plotting: ', vals # The position of the xdata must be calculated for each of the two data # series. xdata = ind + margin + (num * width) # Removing the "align=center" feature will left align graphs, which is # what this method of calculating positions assumes. gene_rects = plt.bar(xdata, vals, width) s.set_xticks(ind + 0.5) s.set_xticklabels(group_labels)

绘图:[23,135,3] ... ValueError:形状不匹配:对象无法广播为单个形状

plotting: [23, 135, 3] ... ValueError: shape mismatch: objects cannot be broadcast to a single shape

推荐答案

使用熊猫:

import pandas as pd groups = [[23,135,3], [123,500,1]] group_labels = ['views', 'orders'] # Convert data to pandas DataFrame. df = pd.DataFrame(groups, index=group_labels).T # Plot. pd.concat( [df.mean().rename('average'), df.min().rename('min'), df.max().rename('max')], axis=1).plot.bar()

更多推荐

分组条形图 pandas

本文发布于:2023-07-18 00:48:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1139262.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条形图   pandas

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!