简体   繁体   中英

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:

..........

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.

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.

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:

import matplotlib.pyplot as plt .

Then, try adding the following line right before plt.show()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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