[英]Grouped Bar graph Pandas
我在一個名為df
DataFrame
有一個表:
+--- -----+------------+-------------+----------+------------+-----------+
|avg_views| avg_orders | max_views |max_orders| min_views |min_orders |
+---------+------------+-------------+----------+------------+-----------+
| 23 | 123 | 135 | 500 | 3 | 1 |
+---------+------------+-------------+----------+------------+-----------+
我現在正在尋找的是繪制一個分組條形圖,它在一個條形圖中向我顯示(平均、最大、最小)視圖和訂單。
即在 x 軸上,視圖和訂單將被分開的距離和 3 條(平均、最大值、最小值)用於視圖和類似的訂單。
我附上了一個示例條形圖圖像,只是為了了解條形圖的外觀。
我從在 matplotlib 中設置分組條形圖之間的間距中獲取了以下代碼,但它對我不起作用:
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:形狀不匹配:對象不能廣播到單個形狀
您不必為了以某種方式繪制它而修改您的數據框,對嗎?
使用seaborn!
import seaborn as sns
sns.catplot(x = "x", # x variable name
y = "y", # y variable name
hue = "type", # group variable name
data = df, # dataframe to plot
kind = "bar")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.