简体   繁体   English

在分组条形图中显示负值的问题(matplotlib)

[英]Problem to display negative values in grouped bar chart (matplotlib)

I have the following Data Frame:我有以下数据框:

                       Location  Growth                Growth_Zero_Migration
0                       Africa  2939529.018            2998261.337
1                         Asia    78852.134             256394.122
2        Australia/New Zealand    18563.010              -2212.990
3  Europe and Northern America     3945.429            -253849.105
4                South America    -1459.056               3117.976

When I try to display it by matplot (as a grouped bar chart ),not all the negative values are shown correctly.I found this solution Negative values bars on the same matplotlib chart , but it didn't help me a lot - all my bars get either bottom or top value of yI guess my problem is a range ( as you can see it's [-253849.105, 2998261.337], but I've no idea how to normalize it. Any hint will be very appreciated. Here is my code and output:当我尝试通过 matplot 显示它(作为分组条形图)时,并非所有负值都正确显示。我发现这个解决方案Negative values bars on the same matplotlib chart ,但它对我没有多大帮助 - 所有我的条获得 y 的底部或顶部值我想我的问题是一个范围(如您所见,它是 [-253849.105, 2998261.337],但我不知道如何对其进行规范化。任何提示将不胜感激。这是我的代码和 output:

..........

def get_Table_For_Growth(columnName, fileName, variant, range):
    pop_stat = pb.read_csv("WPP2019_TotalPopulationBySex.csv")
    locations_table = pb.read_csv("{filename}.csv".format(filename=fileName))
    table = pop_stat[(pop_stat['Variant'] == variant) & (pop_stat[columnName].isin(locations_table[columnName])) & (
            (pop_stat['Time'] == range[0]) | (pop_stat['Time'] == range[1]))].loc[:, ['Location', 'PopTotal']]
    table['Growth'] = table.groupby('Location')['PopTotal'].diff()
    table = table.dropna()
    table = table.reset_index(drop=True)
    # table.style.hide_index()
    table = table.sort_values(by='Growth', ascending=False)
    del table['PopTotal']
    return table


def show_graph(table, type, xcoor, ycoor, colour):
    table.plot(kind=type, x=xcoor, y=ycoor, color=colour)
    plt.show()

continents_zero_migration = get_Table_For_Growth("Location", "continents", "Zero migration", [2020, 2100])
continents_medium_vs_zero_migration = get_Table_For_Growth("Location", "continents", "Medium", [2020, 2100])
continents_medium_vs_zero_migration['Growth_Zero_Migration'] = continents_zero_migration['Growth']
continents_medium_vs_zero_migration = pb.DataFrame({'Growth Forecast': continents_medium_vs_zero_migration['Growth'].tolist(),
                                                     'Zero migration' : continents_medium_vs_zero_migration['Growth_Zero_Migration'].tolist()},
                                                       index = continents_medium_vs_zero_migration['Location'])
continents_medium_vs_zero_migration.plot.bar()
plt.show()
..........

在此处输入图像描述

I believe using plt.yscale('symlog') may help you to get the results you want.我相信使用plt.yscale('symlog')可以帮助您获得所需的结果。

Toy example code玩具示例代码

Below self contained toy example code is a simplified script of your code:下面自包含的玩具示例代码是您的代码的简化脚本:

import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame([['Africa',2939529.018,2998261.337],\
['Asia',78852.134,256394.122],\
['Australia/New Zealand',18563.010,-2212.990],\
['Europe and Northern America',3945.429,-253849.105],\
['South America',-1459.056,3117.976]], columns=['Location','Growth','Growth_Zero_Migration'])
ax = df.plot.bar()
plt.xticks(range(len(df)),df['Location'])
plt.yscale('symlog')
plt.xlabel('Location')
plt.show()

The results is below graph:结果如下图:

在此处输入图像描述

Which, as you can see, is log scaled on y-axis with positive and negative values, and you can easily see the whole data.如您所见,它在 y 轴上以正负值进行对数缩放,您可以轻松查看整个数据。

Adding a grid添加网格

In this case I would recommend to use grid adding following code before showing the graph:在这种情况下,我建议在显示图表之前使用网格添加以下代码:

plt.grid(True)

As values can differ a lot between ranges in the log scale.由于对数刻度范围之间的值可能有很大差异。 And the result graph would be:结果图将是:

在此处输入图像描述

Import pyplot:导入pyplot:

import matplotlib.pyplot as plt . import matplotlib.pyplot as plt

Then, try adding the following line right before plt.show()然后,尝试在plt.show()之前添加以下行

plt.gca().set_ylim(-3E6, 3E6)

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

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