-
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
Open
chinandrew
wants to merge
7
commits into
main
Choose a base branch
from
plotting-ax
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6561e70
add axes argument and return axes
chinandrew 9c146e9
fix tests
chinandrew 516fc30
Add type annotation for ax args
chinandrew 0877ef6
Fix docstrings
chinandrew 96fdf31
fix argument order
chinandrew 4097b64
Fix keyword arg
chinandrew edd9b81
fix limit issue
chinandrew File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: | ||
|
|
@@ -71,6 +72,9 @@ def plot(data: pd.DataFrame, | |
| bubble but have the region displayed in white, and values above the mean + 3 std dev are binned | ||
| into the highest bubble. Bubbles are scaled by area. | ||
|
|
||
| A Matplotlib Axes object can be provided to plot the maps onto an existing figure. Otherwise, | ||
| a new Axes object will be created and returned. | ||
|
|
||
| :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``. | ||
|
|
@@ -79,7 +83,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 +97,31 @@ 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 _plot_background_states(ax=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(). | ||
|
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? 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. |
||
|
|
||
| .. deprecated:: 0.1.1 | ||
| Use ``plot()`` instead. | ||
|
|
||
| :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 +296,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 = (12.8, 9.6), 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 figsize: Dimensions of plot. Ignored if ax is provided. | ||
| :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 | ||
| ax.set_xlim(ax.get_xlim()) | ||
| ax.set_ylim(ax.get_ylim()) | ||
| return ax | ||
|
|
||
|
|
||
| def _project_and_transform(data: gpd.GeoDataFrame, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.