-
Notifications
You must be signed in to change notification settings - Fork 27
Add axes argument and return axes in plots #361
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
6561e70
9c146e9
516fc30
0877ef6
96fdf31
4097b64
edd9b81
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,7 +43,8 @@ def plot(data: pd.DataFrame, | |
| time_value: date = None, | ||
| plot_type: str = "choropleth", | ||
| combine_megacounties: bool = True, | ||
| **kwargs: Any) -> figure.Figure: | ||
| ax: axes.Axes = None, | ||
| **kwargs: Any) -> axes.Axes: | ||
| """Given the output data frame of :py:func:`covidcast.signal`, plot a choropleth or bubble map. | ||
|
|
||
| Projections used for plotting: | ||
|
|
@@ -79,7 +80,8 @@ def plot(data: pd.DataFrame, | |
| Defaults to `True`. | ||
| :param kwargs: Optional keyword arguments passed to ``GeoDataFrame.plot()``. | ||
| :param plot_type: Type of plot to create. Either choropleth (default) or bubble map. | ||
| :return: Matplotlib figure object. | ||
| :param ax: Optional matplotlib axis to plot on. | ||
| :return: Matplotlib axes object. | ||
|
|
||
| """ | ||
| if plot_type not in {"choropleth", "bubble"}: | ||
|
|
@@ -92,26 +94,27 @@ def plot(data: pd.DataFrame, | |
| kwargs["vmax"] = kwargs.get("vmax", meta["mean_value"] + 3 * meta["stdev_value"]) | ||
| kwargs["figsize"] = kwargs.get("figsize", (12.8, 9.6)) | ||
|
|
||
| fig, ax = _plot_background_states(kwargs["figsize"]) | ||
| ax = _plot_background_states(kwargs["figsize"]) if ax is None else ax | ||
| ax.axis("off") | ||
| ax.set_title(f"{data_source}: {signal}, {day_to_plot.strftime('%Y-%m-%d')}") | ||
| if plot_type == "choropleth": | ||
| _plot_choro(ax, day_data, combine_megacounties, **kwargs) | ||
| else: | ||
| _plot_bubble(ax, day_data, geo_type, **kwargs) | ||
| return fig | ||
| return ax | ||
|
|
||
|
|
||
| def plot_choropleth(data: pd.DataFrame, | ||
| time_value: date = None, | ||
| combine_megacounties: bool = True, | ||
| **kwargs: Any) -> figure.Figure: | ||
| **kwargs: Any) -> axes.Axes: | ||
| """Plot choropleths for a signal. This method is deprecated and has been generalized to plot(). | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Want to use the deprecated directive here?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added. This also made me realize our changelog is a bit incorrect, will make another PR to fix that. |
||
|
|
||
| :param data: Data frame of signal values, as returned from :py:func:`covidcast.signal`. | ||
| :param time_value: If multiple days of data are present in ``data``, map only values from this | ||
| day. Defaults to plotting the most recent day of data in ``data``. | ||
| :param kwargs: Optional keyword arguments passed to ``GeoDataFrame.plot()``. | ||
| :return: Matplotlib figure object. | ||
| :return: Matplotlib axes object. | ||
| """ | ||
| warnings.warn("Function `plot_choropleth` is deprecated. Use `plot()` instead.") | ||
| return plot(data, time_value, "choropleth", combine_megacounties, **kwargs) | ||
|
|
@@ -286,21 +289,22 @@ def _plot_bubble(ax: axes.Axes, data: gpd.GeoDataFrame, geo_type: str, **kwargs: | |
| ax.legend(frameon=False, ncol=8, loc="lower center", bbox_to_anchor=(0.5, -0.1)) | ||
|
|
||
|
|
||
| def _plot_background_states(figsize: tuple) -> tuple: | ||
| def _plot_background_states(figsize: tuple, ax: axes.Axes = None) -> axes.Axes: | ||
| """Plot US states in light grey as the background for other plots. | ||
|
|
||
| :param figsize: Dimensions of plot. | ||
|
||
| :return: Matplotlib figure and axes. | ||
| :param ax: Optional matplotlib axis to plot on. | ||
| :return: Matplotlib axes. | ||
| """ | ||
| fig, ax = plt.subplots(1, figsize=figsize) | ||
| ax.axis("off") | ||
| if ax is None: | ||
| fig, ax = plt.subplots(1, figsize=figsize) | ||
| state_shapefile_path = pkg_resources.resource_filename(__name__, SHAPEFILE_PATHS["state"]) | ||
| state = gpd.read_file(state_shapefile_path) | ||
| for state in _project_and_transform(state, "STATEFP"): | ||
| state.plot(color="0.9", ax=ax, edgecolor="0.8", linewidth=0.5) | ||
| ax.set_xlim(plt.xlim()) | ||
| ax.set_ylim(plt.ylim()) | ||
| return fig, ax | ||
| return ax | ||
|
|
||
|
|
||
| def _project_and_transform(data: gpd.GeoDataFrame, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the docstring should say what happens if
axis not provided (axes are made, states are plotted). Maybe that goes in the text above, such as in a paragraph after the one-line summary explaining that the maps are plotted on US states by default.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I had a bug here, the background states are always plotted and I had left that statement out. I've also added a sentence describing the return behavior.