IAMconsortium / pyam

Analysis & visualization of energy & climate scenarios
https://pyam-iamc.readthedocs.io/
Apache License 2.0
226 stars 118 forks source link

Clustered stacked bar when comparing scenarios #603

Closed IoannisDafnomilis closed 2 years ago

IoannisDafnomilis commented 2 years ago

Hello all and thank you for the great work.

I have been using pyam the past couple of weeks and had no problem with anything until recently. I'm trying to produce a clustered stacked bar plot for comparing the same variables for the same year between different scenarios.

The code im using is:

data = df_nz.filter(**args, variable=("Emissions|CH4", "Emissions|HFC", "Emissions|N2O",
                                      "Emissions|PFC", "Emissions|SF6")).filter(region=("China"), keep=True)
ax = data.plot.bar(bars="variable", x="year", stacked=True, position=1.5, width=.2, cmap="tab20",linewidth=0.5,edgecolor = "black")

args2 = dict(scenario=("B"), year=[2015,2030, 2050])
data2 = df_nz.filter(**args2, variable=("Emissions|CH4", "Emissions|HFC", "Emissions|N2O",
                                      "Emissions|PFC", "Emissions|SF6")).filter(region=("China"), keep=True)
data2.plot.bar(ax=ax, bars="variable", x="year", stacked=True, position=-0.5, width=.2, cmap="tab20",linewidth=0.5,edgecolor = "black")

args3 = dict(scenario=("C"), year=[2015,2030, 2050])
data3 = df_nz.filter(**args3, variable=("Emissions|CH4", "Emissions|HFC", "Emissions|N2O",
                                      "Emissions|PFC", "Emissions|SF6")).filter(region=("China"), keep=True)
data3.plot.bar(ax=ax, bars="variable", x="year", stacked=True, position=0.5, width=.2, cmap="tab20",linewidth=0.5,edgecolor = "black")

plt.show()

So this produces a graph which is almost what I want but not exactly (see below). the 2015 values are the same for all scenarios so ideally only 1 of the 3 bars should be present there, but changing the 'year' for 2 of the scenarios overwrites the other one so you end up with 2030 and 2050 bars only.

Additionally the graph shows only the year on the x-axis. I would like to add the scenario per respective column name as well.

I have tried everything I could think on the above (and checked pyam's documentation as well) but nothing seems to work exactly how I would like to.

Any ideas or documentation on this I might have missed?

Thank you!

Graph_github

danielhuppmann commented 2 years ago

Thanks for the feedback, @gdafn, and happy to see that you find the package useful!

For this particular use case, you are pushing the pyam plotting gallery beyond its current scope: I don't think that multi-tiered x-axis labels are supported by the connection between pyam and matplotlib. If you want to explore how to implement this, I'm happy to review any PRs (or guide you in how to get started with submitting a feature extension to the repo).

As a quick-fix: you could create a new scenario (called "reference" or "history") which only has the 2015 values, and then you plot that reference scenario together with the scenario data for 2030 and 2050 - maybe this produces a figure closer to what you have in mind...

IoannisDafnomilis commented 2 years ago

Thanks for the reply @danielhuppmann ! I will try to implement the quick-fix, I had already started on that indeed.

danielhuppmann commented 2 years ago

Please join our mailing list of Slack workspace for updates, new releases or discussing follow-up questions! https://pyam-iamc.readthedocs.io/en/stable/contributing.html