简体   繁体   English

plot 条形图网格或包装条形图使用 python (matplotlib)

[英]plot grid of bar charts or wrap bar chart using python (matplotlib)

I want to create horizontal bar chart using python, but there are too many categories like this我想用python制作水平条形图,但是这样的分类太多了

import matplotlib.pyplot as plt
x = ['c0', 'c1', 'c2', 'c3', 'c4',
     'c5', 'c6', 'c7', 'c8', 'c9',
     'c10', 'c11', 'c12', 'c13', 'c14',
     'c15', 'c16', 'c17', 'c18', 'c19',
     'c20', 'c21', 'c22', 'c23', 'c24',
     'c25', 'c26', 'c27', 'c28', 'c29']
    
 y = [9556, 9318, 9308, 2556, 2424, 1812, 1624, 1468, 1398, 1344, 1200,
    1056, 888, 732, 732, 704, 684, 680, 666, 660, 636, 624,
    576, 552, 544, 532, 468, 448, 448, 436]
    
 plt.barh(new_x, new_y)

在此处输入图像描述 The above code is only a part of the categories, in fact, the number of my categories is more than 200. I want to cut into two subplots to present, like this上面的代码只是分类的一部分,其实我的分类有200多个,我想切分成两个子图来呈现, 像这样

在此处输入图像描述

Is there any way to plot the above graph in python? python中的上图plot有什么办法吗?

Use an index to split your data, and a vertical subplot to plot you data.使用索引拆分数据,并使用垂直子图将数据拆分为 plot。 Eg something like this:例如这样的事情:

split = len(x) // 2
fig, (ax1, ax2) = plt.subplots(1, 2, sharex=True, sharey=False, figsize=(10, 8))


x = ['c0', 'c1', 'c2', 'c3', 'c4',
     'c5', 'c6', 'c7', 'c8', 'c9',
     'c10', 'c11', 'c12', 'c13', 'c14',
     'c15', 'c16', 'c17', 'c18', 'c19',
     'c20', 'c21', 'c22', 'c23', 'c24',
     'c25', 'c26', 'c27', 'c28', 'c29']
    
y = [9556, 9318, 9308, 2556, 2424, 1812, 1624, 1468, 1398, 1344, 1200,
1056, 888, 732, 732, 704, 684, 680, 666, 660, 636, 624,
576, 552, 544, 532, 468, 448, 448, 436]

ax1.barh(x[0:split], y[0:split])
ax2.barh(x[split:], y[split:])

Result:结果: 在此处输入图像描述

Tweak the plot further to get your plots the same as your example.进一步调整 plot 以使您的绘图与您的示例相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM