简体   繁体   English

Python Plot无法正常工作

[英]Python Plot does not work correctly

I'm new to Python and I'm trying to plot some data with the help of matplotlib. 我是Python的新手,我试图在matplotlib的帮助下绘制一些数据。

I'm trying to group the data but the problem is that the groups overlap each other. 我正在尝试对数据进行分组,但问题是这些组相互重叠。 Here is a picture which describes my problem: Problem 这是描述我的问题的图片: 问题

问题

Here is my code: 这是我的代码:

import numpy as np
import matplotlib.pyplot as plt

n_groups = 3
credits = (market[0], market[1], market[2])
debits = (dmarket[0], dmarket[1], dmarket[2])
profits = (pmarket[0], pmarket[1], pmarket[2])
fig, ax = plt.subplots()
index = np.arange(n_groups)
bar_width = 0.45
opacity = 0.4
error_config = {'ecolor': '0.3'}

rectsCredits = plt.bar(index, credits, bar_width,
                 alpha=opacity,
                 color='b',
                 error_kw=error_config,
                 label='Credit')

rectsDebits = plt.bar(index + bar_width, debits, bar_width,
                 alpha=opacity,
                 color='r',
                 error_kw=error_config,
                 label='Debit')

rectsProfits = plt.bar(index + 2*bar_width, profits, bar_width,
                 alpha=opacity,
                 color='g',
                 error_kw=error_config,
                 label='Profits')

plt.xticks(index + bar_width/2, ('Tariff Market', 'Wholesale Market', 'Balancing Market'))
plt.legend()
plt.tight_layout()

def autolabel(rects):
    """
    Attach a text label above each bar displaying its height
    """
    for rect in rects:
        height = rect.get_height()
        ax.text(rect.get_x() + rect.get_width() / 2.,
                1.01 * height,
                '%d' % int(height),
                ha='center', va='bottom')

autolabel(rectsCredits)
autolabel(rectsDebits)
autolabel(rectsProfits)

plt.show()

I don't know what to do. 我不知道该怎么办。 I think there is only a little logic problem which I don't see right now! 我认为现在还没有看到一点逻辑问题!

The position of the bars is a bit off. 酒吧的位置有点偏。 You insert the first label group at [0, 1, 2] ( index ), the second on [0.45, 1.45, 2.45] ( index + bar_width ) and the third at [0.9, 1.9, 2.9] ( index + 2*bar_width ). 你在插入所述第一标签组[0, 1, 2] index ),该第二上[0.45, 1.45, 2.45] index + bar_width ),并在第三[0.9, 1.9, 2.9] index + 2*bar_width )。 Each bar has a width of 0.45 so no wonder these overlap. 每个条的宽度为0.45因此难怪这些重叠。

For the following part I chose just some data for visualization, you have to insert or use the correct values. 对于以下部分,我只选择了一些可视化数据,您必须插入或使用正确的值。

If you change bar_width to 1/3 then there's no empty space between the groups: 如果将bar_width更改为1/3则组之间没有空格:

bar_width = 1 / 3

在此输入图像描述

If you choose something like 1/4 then it will have exactly the space for one additional bar between each group: 如果您选择1/4东西,那么它将在每个组之间有一个额外的条形空间:

bar_width = 1 / 4

在此输入图像描述

But the label isn't centered correctly yet, but that can be easily fixed by using a new index in plt.xticks : 但标签尚未正确居中,但可以通过在plt.xticks使用新索引轻松修复:

bar_width = 1 / 4
plt.xticks(index + bar_width, ('Tariff Market', 'Wholesale Market', 'Balancing Market'))

在此输入图像描述

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

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