Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable colors as list in plot.pie() #563

Merged
merged 5 commits into from
Jul 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ the attribute `_LONG_IDX` as deprecated. Please use `dimensions` instead.
## Individual updates

- [#564](https://github.com/IAMconsortium/pyam/pull/564) Add an example with a secondary axis to the plotting gallery
- [#563](https://github.com/IAMconsortium/pyam/pull/563) Enable `colors` keyword argument as list in `plot.pie()`
- [#560](https://github.com/IAMconsortium/pyam/pull/560) Add a feature to `swap_year_for_time()`
- [#559](https://github.com/IAMconsortium/pyam/pull/559) Add attribute `dimensions`, fix compatibility with pandas v1.3
- [#557](https://github.com/IAMconsortium/pyam/pull/557) Swap time for year keeping subannual resolution
Expand Down
43 changes: 24 additions & 19 deletions pyam/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def pie(
value="value",
category="variable",
legend=False,
title=True,
title=None,
ax=None,
cmap=None,
**kwargs,
Expand All @@ -291,8 +291,8 @@ def pie(
The column to use for labels
legend : bool, optional
Include a legend.
title : bool or string, optional
Display a default or custom title.
title : string, optional
Text to use for the title.
ax : :class:`matplotlib.axes.Axes`, optional
cmap : string, optional
The name of a registered colormap.
Expand All @@ -313,8 +313,7 @@ def pie(
for col in set(SORT_IDX) - set([category]):
if len(df[col].unique()) > 1:
msg = (
"Can not plot multiple {}s in a pie plot with value={},"
+ " category={}"
"Can not plot multiple {}s in a pie plot with value={} and category={}"
)
raise ValueError(msg.format(col, value, category))

Expand All @@ -332,19 +331,25 @@ def pie(
"color"
]
rc = run_control()
color = []
for key, c in zip(_df.index, defaults):
if "color" in rc and category in rc["color"] and key in rc["color"][category]:
c = rc["color"][category][key]
color.append(c)

if "colors" in kwargs:
colors = kwargs.pop("colors")
else:
colors = []
for key, c in zip(_df.index, defaults):
if category in rc["color"] and key in rc["color"][category]:
c = rc["color"][category][key]
colors.append(c)

# plot data
_df.plot(kind="pie", colors=color, ax=ax, explode=explode, **kwargs)
_df.plot(kind="pie", colors=colors, ax=ax, explode=explode, **kwargs)

# add legend
# add legend and title
ax.legend(loc="center left", bbox_to_anchor=(1.0, 0.5), labels=_df.index)
if not legend:
ax.legend_.remove()
if title:
ax.set_title(title)

# remove label
ax.set_ylabel("")
Expand Down Expand Up @@ -390,7 +395,7 @@ def stack(
legend : bool, optional
Include a legend.
title : bool or string, optional
Display a default or custom title.
Text to use for the title, display a default if True.
ax : :class:`matplotlib.axes.Axes`, optional
cmap : string, optional
The name of a registered colormap.
Expand Down Expand Up @@ -559,7 +564,7 @@ def bar(
legend : bool, optional
Include a legend.
title : bool or string, optional
Display a default or custom title.
Text to use for the title, display a default if True.
ax : :class:`matplotlib.axes.Axes`, optional
cmap : string, optional
The name of a registered colormap.
Expand Down Expand Up @@ -661,7 +666,7 @@ def box(df, y="value", x=None, by=None, legend=True, title=None, ax=None, **kwar
legend : bool, optional
Include a legend.
title : bool or string, optional
Display a default or custom title.
Text to use for the title, display a default if True.
ax : :class:`matplotlib.axes.Axes`, optional
kwargs
Additional arguments passed to :meth:`pandas.DataFrame.plot`.
Expand Down Expand Up @@ -776,7 +781,7 @@ def scatter(
If a dictionary is provided, it will be used as keyword arguments
in creating the legend.
title : bool or string, optional
Display a custom title.
Text to use for the title, display a default if True.
color : string, optional
A valid matplotlib color or column name. If a column name, common
values will be provided the same color.
Expand Down Expand Up @@ -937,7 +942,7 @@ def line(
If a dictionary is provided, it will be used as keyword arguments
in creating the legend.
title : bool or string, optional
Display a default or custom title.
Text to use for the title, display a default if True.
color : string, optional
A valid matplotlib color or column name. If a column name, common
values will be provided the same color.
Expand Down Expand Up @@ -1081,7 +1086,7 @@ def line(
.T.interpolate(method="index")
.T # interpolate
)
mins = pd.concat([allmins, intermins]).min(level=0)
mins = pd.concat([allmins, intermins]).groupby(level=0).min()
allmaxs = data.groupby(color).max()
intermaxs = (
data.dropna(axis=1)
Expand All @@ -1091,7 +1096,7 @@ def line(
.T.interpolate(method="index")
.T # interpolate
)
maxs = pd.concat([allmaxs, intermaxs]).max(level=0)
maxs = pd.concat([allmaxs, intermaxs]).groupby(level=0).max()
# do the fill
for idx in mins.index:
ymin = mins.loc[idx]
Expand Down
Binary file added tests/expected_figs/test_pie_plot_colors.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/expected_figs/test_pie_plot_other.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,15 @@ def test_pie_plot_legend(plot_df):
return fig


@pytest.mark.mpl_image_compare(**MPL_KWARGS)
def test_pie_plot_colors(plot_df):
fig, ax = plt.subplots(figsize=(8, 8))
plot_df.filter(variable="Primary Energy", model="test_model", year=2010).plot.pie(
ax=ax, category="scenario", colors=["green", "yellow"], title="foo"
)
return fig


@pytest.mark.mpl_image_compare(**MPL_KWARGS)
def test_pie_plot_other(plot_df):
fig, ax = plt.subplots(figsize=(8, 8))
Expand Down