Python 使用matplotlib从嵌套字典打印条形图
我有一个嵌套的dict,看起来像这样Python 使用matplotlib从嵌套字典打印条形图,python,matplotlib,Python,Matplotlib,我有一个嵌套的dict,看起来像这样 {A: { 'apples': 3, 'bananas':5, 'oranges':6, 'kiwis':9}, B: { 'apples': 1, 'bananas':9, 'oranges':3, 'kiwis':1}, C: { 'apples': 6, 'bananas':9, 'oranges':3,
{A: { 'apples': 3,
'bananas':5,
'oranges':6,
'kiwis':9},
B: { 'apples': 1,
'bananas':9,
'oranges':3,
'kiwis':1},
C: { 'apples': 6,
'bananas':9,
'oranges':3,
'kiwis':3}}
在我的例子中,A、B、C是一年中的几个月,持续两年。x轴表示月份,即A、B、C等。苹果、香蕉、猕猴桃和橙子为计数。
我想使用matplotlib绘制分组垂直条形图。它将有一个苹果、香蕉和橘子三种颜色的传说
我只能使用dataframe.plot方法进行绘图:
pd.Dataframe(mydict).T.plot(kind=bar)
我希望能够使用matplotlib绘制相同的图形,以便管理图形大小和更改条形图的大小等
感谢您的帮助
谢谢下面是示例代码。如果您有任何问题,请告诉我,我将非常乐意帮助您
# libraries
import numpy as np
import matplotlib.pyplot as plt
# set width of bar
barWidth = 0.25
# set height of bar
bars1 = [12, 30, 1, 8, 22]
bars2 = [28, 6, 16, 5, 10]
bars3 = [29, 3, 24, 25, 17]
# Set position of bar on X axis
r1 = np.arange(len(bars1))
r2 = [x + barWidth for x in r1]
r3 = [x + barWidth for x in r2]
# Make the plot
plt.bar(r1, bars1, color='#ff0000', width=barWidth, edgecolor='red', label='Apples')
plt.bar(r2, bars2, color='#FFFF00', width=barWidth, edgecolor='yellow', label='bananas')
plt.bar(r3, bars3, color='#FFA500', width=barWidth, edgecolor='orange', label='oranges')
# Add xticks on the middle of the group bars
plt.xlabel('group', fontweight='bold')
plt.xticks([r + barWidth for r in range(len(bars1))], ['04/01/2019', '04/02/2019', '04/03/2019', '04/04/2019', '04/05/2019'])
# Create legend & Show graphic
plt.legend()
plt.show()
下面是示例代码。如果您有任何问题,请告诉我,我将非常乐意帮助您
# libraries
import numpy as np
import matplotlib.pyplot as plt
# set width of bar
barWidth = 0.25
# set height of bar
bars1 = [12, 30, 1, 8, 22]
bars2 = [28, 6, 16, 5, 10]
bars3 = [29, 3, 24, 25, 17]
# Set position of bar on X axis
r1 = np.arange(len(bars1))
r2 = [x + barWidth for x in r1]
r3 = [x + barWidth for x in r2]
# Make the plot
plt.bar(r1, bars1, color='#ff0000', width=barWidth, edgecolor='red', label='Apples')
plt.bar(r2, bars2, color='#FFFF00', width=barWidth, edgecolor='yellow', label='bananas')
plt.bar(r3, bars3, color='#FFA500', width=barWidth, edgecolor='orange', label='oranges')
# Add xticks on the middle of the group bars
plt.xlabel('group', fontweight='bold')
plt.xticks([r + barWidth for r in range(len(bars1))], ['04/01/2019', '04/02/2019', '04/03/2019', '04/04/2019', '04/05/2019'])
# Create legend & Show graphic
plt.legend()
plt.show()
首先,您可以使用
figsize
参数管理figsize,或者将.plot
方法返回的轴存储在数据帧上,因此纯matplotlib
解决方案并不是唯一的方法
话虽如此。。。学习matplotlib中分组条形图的重要方法是设置偏移量。每组分组条(例如苹果条)需要根据宽度(例如宽度*2)从XTICK偏移
首先,您可以使用figsize
参数管理figsize,或者将.plot
方法返回的轴存储在数据帧上,因此纯matplotlib
解决方案并不是唯一的方法
话虽如此。。。学习matplotlib中分组条形图的重要方法是设置偏移量。每组分组条(例如苹果条)需要根据宽度(例如宽度*2)从XTICK偏移
使用matplotlib确实可以更好地控制地物的元素(首先是),但如果这仅适用于地物大小和条形图大小,则可以直接在pandas plot函数中使用figsize=(12,8)
和width=0.6
。使用matplotlib确实可以更好地控制地物的元素(一开始是)但如果这仅用于图形大小和条形图大小,则可以直接在pandas plot函数中使用figsize=(12,8)
和width=0.6
。这不是从嵌套的类似于op ASQUEST的dict生成绘图。这不是从嵌套的类似于op ASQUEST的dict生成绘图。