-
Notifications
You must be signed in to change notification settings - Fork 300
Zonal mean gallery example #4871
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
Changes from all commits
9e96fc1
55e8f38
6379060
2320616
35affef
96ccb00
6a538fd
e541085
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 | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,75 @@ | ||||||||||
| """ | ||||||||||
| Zonal Mean Diagram of Air Temperature | ||||||||||
| ===================================== | ||||||||||
|
|
||||||||||
| This example demonstrates aligning a linear plot and a cartographic plot using Matplotlib. | ||||||||||
|
|
||||||||||
| """ | ||||||||||
|
|
||||||||||
| import cartopy.crs as ccrs | ||||||||||
| import matplotlib.pyplot as plt | ||||||||||
| from mpl_toolkits.axes_grid1 import make_axes_locatable | ||||||||||
| import numpy as np | ||||||||||
|
|
||||||||||
| import iris | ||||||||||
| from iris.analysis import MEAN | ||||||||||
| import iris.plot as iplt | ||||||||||
|
|
||||||||||
|
|
||||||||||
| def main(): | ||||||||||
| fname = iris.sample_data_path("air_temp.pp") | ||||||||||
| temperature = iris.load_cube(fname) | ||||||||||
| collapsed_temp = temperature.collapsed("longitude", MEAN) | ||||||||||
|
|
||||||||||
| # Set y axes with -90 and 90 limits and spacing of 15 per tick. | ||||||||||
| yticks = np.arange(-90, 105, 15) | ||||||||||
| ylim = [-90, 90] | ||||||||||
| fig = plt.figure(figsize=[12, 4]) | ||||||||||
| ax1 = fig.add_subplot(111, projection=ccrs.PlateCarree()) | ||||||||||
| plt.sca(ax1) | ||||||||||
| im = iplt.contourf(temperature, cmap="RdYlBu_r") | ||||||||||
| ax1.coastlines() | ||||||||||
| ax1.gridlines() | ||||||||||
| ax1.set_xticks([-180, -90, 0, 90, 180], crs=ccrs.PlateCarree()) | ||||||||||
| ax1.set_yticks(yticks, crs=ccrs.PlateCarree()) | ||||||||||
| ax1.set_title("Air Temperature") | ||||||||||
| ax1.set_ylabel("latitude") | ||||||||||
| ax1.set_xlabel("longitude") | ||||||||||
| ax1.set_ylim(*ylim) | ||||||||||
| divider = make_axes_locatable(ax1) | ||||||||||
|
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. This is the 'magic sauce' that makes it possible to align everything, so it should have some extra emphasis. I've done a bit of reading to work out an appropriate comment:
Suggested change
|
||||||||||
|
|
||||||||||
| # Gives the air temperature bar size, colour and a title. | ||||||||||
| ax2 = divider.new_vertical( | ||||||||||
| size="5%", pad=0.5, axes_class=plt.Axes, pack_start=True | ||||||||||
| ) | ||||||||||
| fig.add_axes(ax2) | ||||||||||
| plt.sca(ax2) | ||||||||||
| cbar = plt.colorbar(im, cax=ax2, orientation="horizontal") | ||||||||||
| cbar.ax.set_xlabel("Air Temperature [k]") | ||||||||||
|
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.
Suggested change
|
||||||||||
|
|
||||||||||
| # Round each tick for the third ax to the nearest 20 (ready for use). | ||||||||||
| data_max = collapsed_temp.data.max() | ||||||||||
| x_max = data_max - data_max % -20 | ||||||||||
| data_min = collapsed_temp.data.min() | ||||||||||
| x_min = data_min - data_min % 20 | ||||||||||
|
Comment on lines
+50
to
+54
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. You could move this block after the |
||||||||||
|
|
||||||||||
| # Plot "collapsed_temp" on the mean graph and set the ticks and titles on the axes. | ||||||||||
| ax3 = divider.new_horizontal(size="30%", pad=0.4, axes_class=plt.Axes) | ||||||||||
| fig.add_axes(ax3) | ||||||||||
| plt.sca(ax3) | ||||||||||
| iplt.plot(collapsed_temp, collapsed_temp.coord("latitude")) | ||||||||||
| ax3.axvline(0, color="k", linewidth=0.5) | ||||||||||
| ax3.set_ylim(*ylim) | ||||||||||
|
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. Swap position with |
||||||||||
| ax3.set_title("Zonal mean") | ||||||||||
| ax3.set_ylabel("latitude") | ||||||||||
| ax3.set_xlabel("Air Temperature [k]") | ||||||||||
|
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.
Suggested change
|
||||||||||
| ax3.yaxis.set_label_position("right") | ||||||||||
| ax3.yaxis.tick_right() | ||||||||||
| ax3.set_yticks(yticks) | ||||||||||
| ax3.set_xlim(x_min, x_max) | ||||||||||
|
|
||||||||||
| plt.show() | ||||||||||
|
|
||||||||||
|
|
||||||||||
| if __name__ == "__main__": | ||||||||||
| main() | ||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # Copyright Iris contributors | ||
| # | ||
| # This file is part of Iris and is released under the LGPL license. | ||
| # See COPYING and COPYING.LESSER in the root of the repository for full | ||
| # licensing details. | ||
|
|
||
| # Import Iris tests first so that some things can be initialised before | ||
| # importing anything else. | ||
| import iris.tests as tests | ||
|
|
||
| from .gallerytest_util import ( | ||
| add_gallery_to_path, | ||
| fail_any_deprecation_warnings, | ||
| show_replaced_by_check_graphic, | ||
| ) | ||
|
|
||
|
|
||
| class TestGlobalMap(tests.GraphicsTest): | ||
| """Test the zonal mean gallery code.""" | ||
|
|
||
| def test_plot_zonal_mean(self): | ||
| with fail_any_deprecation_warnings(): | ||
| with add_gallery_to_path(): | ||
| import plot_zonal_mean | ||
| with show_replaced_by_check_graphic(self): | ||
| plot_zonal_mean.main() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| tests.main() |
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.
This deserves its own code block, as it's not directly related to the following code.