2

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 y .I 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()
..........

enter image description here

2 Answers 2

1

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:

enter image description here

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:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

Import pyplot:

import matplotlib.pyplot as plt.

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

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

1 Comment

I don't believe this is a solution to the question OP is requesting. This would only change the limits of the y-axis. OP is asking to be able to see differences between values which differ in various orders of magnitude. I believe y-axis log scale is the way to go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.