@@ -99,6 +99,7 @@ One Dimension
99
99
The simplest way to make a plot is to call the :py:func: `DataArray.plot() ` method.
100
100
101
101
.. ipython :: python
102
+ :okwarning:
102
103
103
104
air1d = air.isel(lat = 10 , lon = 10 )
104
105
@@ -125,6 +126,7 @@ can be used:
125
126
.. _matplotlib.pyplot.plot : http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.plot
126
127
127
128
.. ipython :: python
129
+ :okwarning:
128
130
129
131
@savefig plotting_1d_additional_args.png width =4in
130
132
air1d[:200 ].plot.line(" b-^" )
@@ -137,6 +139,7 @@ can be used:
137
139
Keyword arguments work the same way, and are more explicit.
138
140
139
141
.. ipython :: python
142
+ :okwarning:
140
143
141
144
@savefig plotting_example_sin3.png width =4in
142
145
air1d[:200 ].plot.line(color = " purple" , marker = " o" )
@@ -151,6 +154,7 @@ In this example ``axes`` is an array consisting of the left and right
151
154
axes created by ``plt.subplots ``.
152
155
153
156
.. ipython :: python
157
+ :okwarning:
154
158
155
159
fig, axes = plt.subplots(ncols = 2 )
156
160
@@ -178,6 +182,7 @@ support the ``aspect`` and ``size`` arguments which control the size of the
178
182
resulting image via the formula ``figsize = (aspect * size, size) ``:
179
183
180
184
.. ipython :: python
185
+ :okwarning:
181
186
182
187
air1d.plot(aspect = 2 , size = 3 )
183
188
@savefig plotting_example_size_and_aspect.png
@@ -219,6 +224,7 @@ without coordinates along the x-axis. To illustrate this, let's calculate a 'dec
219
224
from the time and assign it as a non-dimension coordinate:
220
225
221
226
.. ipython :: python
227
+ :okwarning:
222
228
223
229
decimal_day = (air1d.time - air1d.time[0 ]) / pd.Timedelta(" 1d" )
224
230
air1d_multi = air1d.assign_coords(decimal_day = (" time" , decimal_day))
@@ -227,20 +233,23 @@ from the time and assign it as a non-dimension coordinate:
227
233
To use ``'decimal_day' `` as x coordinate it must be explicitly specified:
228
234
229
235
.. ipython :: python
236
+ :okwarning:
230
237
231
238
air1d_multi.plot(x = " decimal_day" )
232
239
233
240
Creating a new MultiIndex named ``'date' `` from ``'time' `` and ``'decimal_day' ``,
234
241
it is also possible to use a MultiIndex level as x-axis:
235
242
236
243
.. ipython :: python
244
+ :okwarning:
237
245
238
246
air1d_multi = air1d_multi.set_index(date = (" time" , " decimal_day" ))
239
247
air1d_multi.plot(x = " decimal_day" )
240
248
241
249
Finally, if a dataset does not have any coordinates it enumerates all data points:
242
250
243
251
.. ipython :: python
252
+ :okwarning:
244
253
245
254
air1d_multi = air1d_multi.drop(" date" )
246
255
air1d_multi.plot()
@@ -256,6 +265,7 @@ with appropriate arguments. Consider the 3D variable ``air`` defined above. We c
256
265
plots to check the variation of air temperature at three different latitudes along a longitude line:
257
266
258
267
.. ipython :: python
268
+ :okwarning:
259
269
260
270
@savefig plotting_example_multiple_lines_x_kwarg.png
261
271
air.isel(lon = 10 , lat = [19 , 21 , 22 ]).plot.line(x = " time" )
@@ -277,6 +287,7 @@ If required, the automatic legend can be turned off using ``add_legend=False``.
277
287
It is also possible to make line plots such that the data are on the x-axis and a dimension is on the y-axis. This can be done by specifying the appropriate ``y `` keyword argument.
278
288
279
289
.. ipython :: python
290
+ :okwarning:
280
291
281
292
@savefig plotting_example_xy_kwarg.png
282
293
air.isel(time = 10 , lon = [10 , 11 ]).plot(y = " lat" , hue = " lon" )
@@ -299,6 +310,7 @@ The argument ``where`` defines where the steps should be placed, options are
299
310
when plotting data grouped with :py:meth: `Dataset.groupby_bins `.
300
311
301
312
.. ipython :: python
313
+ :okwarning:
302
314
303
315
air_grp = air.mean([" time" , " lon" ]).groupby_bins(" lat" , [0 , 23.5 , 66.5 , 90 ])
304
316
air_mean = air_grp.mean()
@@ -321,6 +333,7 @@ Other axes kwargs
321
333
The keyword arguments ``xincrease `` and ``yincrease `` let you control the axes direction.
322
334
323
335
.. ipython :: python
336
+ :okwarning:
324
337
325
338
@savefig plotting_example_xincrease_yincrease_kwarg.png
326
339
air.isel(time = 10 , lon = [10 , 11 ]).plot.line(
@@ -340,6 +353,7 @@ Two Dimensions
340
353
The default method :py:meth: `DataArray.plot ` calls :py:func: `xarray.plot.pcolormesh ` by default when the data is two-dimensional.
341
354
342
355
.. ipython :: python
356
+ :okwarning:
343
357
344
358
air2d = air.isel(time = 500 )
345
359
@@ -350,6 +364,7 @@ All 2d plots in xarray allow the use of the keyword arguments ``yincrease``
350
364
and ``xincrease ``.
351
365
352
366
.. ipython :: python
367
+ :okwarning:
353
368
354
369
@savefig 2d_simple_yincrease.png width=4in
355
370
air2d.plot(yincrease = False )
@@ -369,6 +384,7 @@ and ``xincrease``.
369
384
xarray plots data with :ref: `missing_values `.
370
385
371
386
.. ipython :: python
387
+ :okwarning:
372
388
373
389
bad_air2d = air2d.copy()
374
390
@@ -386,6 +402,7 @@ It's not necessary for the coordinates to be evenly spaced. Both
386
402
produce plots with nonuniform coordinates.
387
403
388
404
.. ipython :: python
405
+ :okwarning:
389
406
390
407
b = air2d.copy()
391
408
# Apply a nonlinear transformation to one of the coords
@@ -402,6 +419,7 @@ Since this is a thin wrapper around matplotlib, all the functionality of
402
419
matplotlib is available.
403
420
404
421
.. ipython :: python
422
+ :okwarning:
405
423
406
424
air2d.plot(cmap = plt.cm.Blues)
407
425
plt.title(" These colors prove North America\n has fallen in the ocean" )
@@ -421,6 +439,7 @@ matplotlib is available.
421
439
``d_ylog.plot() `` updates the xlabel.
422
440
423
441
.. ipython :: python
442
+ :okwarning:
424
443
425
444
plt.xlabel(" Never gonna see this." )
426
445
air2d.plot()
@@ -436,6 +455,7 @@ xarray borrows logic from Seaborn to infer what kind of color map to use. For
436
455
example, consider the original data in Kelvins rather than Celsius:
437
456
438
457
.. ipython :: python
458
+ :okwarning:
439
459
440
460
@savefig plotting_kelvin.png width =4in
441
461
airtemps.air.isel(time = 0 ).plot()
@@ -454,6 +474,7 @@ Here we add two bad data points. This affects the color scale,
454
474
washing out the plot.
455
475
456
476
.. ipython :: python
477
+ :okwarning:
457
478
458
479
air_outliers = airtemps.air.isel(time = 0 ).copy()
459
480
air_outliers[0 , 0 ] = 100
@@ -469,6 +490,7 @@ This will use the 2nd and 98th
469
490
percentiles of the data to compute the color limits.
470
491
471
492
.. ipython :: python
493
+ :okwarning:
472
494
473
495
@savefig plotting_robust2.png width =4in
474
496
air_outliers.plot(robust = True )
@@ -487,6 +509,7 @@ rather than the default continuous colormaps that matplotlib uses. The
487
509
colormaps. For example, to make a plot with 8 discrete color intervals:
488
510
489
511
.. ipython :: python
512
+ :okwarning:
490
513
491
514
@savefig plotting_discrete_levels.png width =4in
492
515
air2d.plot(levels = 8 )
@@ -495,13 +518,15 @@ It is also possible to use a list of levels to specify the boundaries of the
495
518
discrete colormap:
496
519
497
520
.. ipython :: python
521
+ :okwarning:
498
522
499
523
@savefig plotting_listed_levels.png width =4in
500
524
air2d.plot(levels = [0 , 12 , 18 , 30 ])
501
525
502
526
You can also specify a list of discrete colors through the ``colors `` argument:
503
527
504
528
.. ipython :: python
529
+ :okwarning:
505
530
506
531
flatui = [" #9b59b6" , " #3498db" , " #95a5a6" , " #e74c3c" , " #34495e" , " #2ecc71" ]
507
532
@savefig plotting_custom_colors_levels.png width =4in
@@ -559,13 +584,15 @@ arguments to the xarray plotting methods/functions. This returns a
559
584
:py:class: `xarray.plot.FacetGrid ` object.
560
585
561
586
.. ipython :: python
587
+ :okwarning:
562
588
563
589
@savefig plot_facet_dataarray.png
564
590
g_simple = t.plot(x = " lon" , y = " lat" , col = " time" , col_wrap = 3 )
565
591
566
592
Faceting also works for line plots.
567
593
568
594
.. ipython :: python
595
+ :okwarning:
569
596
570
597
@savefig plot_facet_dataarray_line.png
571
598
g_simple_line = t.isel(lat = slice (0 , None , 4 )).plot(
@@ -582,6 +609,7 @@ a fixed amount. Now we can see how the temperature maps would compare if
582
609
one were much hotter.
583
610
584
611
.. ipython :: python
612
+ :okwarning:
585
613
586
614
t2 = t.isel(time = slice (0 , 2 ))
587
615
t4d = xr.concat([t2, t2 + 40 ], pd.Index([" normal" , " hot" ], name = " fourth_dim" ))
@@ -603,6 +631,7 @@ Faceted plotting supports other arguments common to xarray 2d plots.
603
631
plt.close(" all" )
604
632
605
633
.. ipython :: python
634
+ :okwarning:
606
635
607
636
hasoutliers = t.isel(time = slice (0 , 5 )).copy()
608
637
hasoutliers[0 , 0 , 0 ] = - 100
@@ -649,6 +678,7 @@ Here is an example of using the lower level API and then modifying the axes afte
649
678
they have been plotted.
650
679
651
680
.. ipython :: python
681
+ :okwarning:
652
682
653
683
g = t.plot.imshow(" lon" , " lat" , col = " time" , col_wrap = 3 , robust = True )
654
684
@@ -688,13 +718,15 @@ Consider this dataset
688
718
Suppose we want to scatter ``A `` against ``B ``
689
719
690
720
.. ipython :: python
721
+ :okwarning:
691
722
692
723
@savefig ds_simple_scatter.png
693
724
ds.plot.scatter(x = " A" , y = " B" )
694
725
695
726
The ``hue `` kwarg lets you vary the color by variable value
696
727
697
728
.. ipython :: python
729
+ :okwarning:
698
730
699
731
@savefig ds_hue_scatter.png
700
732
ds.plot.scatter(x = " A" , y = " B" , hue = " w" )
@@ -705,6 +737,7 @@ You can force a legend instead of a colorbar by setting ``hue_style='discrete'``
705
737
Additionally, the boolean kwarg ``add_guide `` can be used to prevent the display of a legend or colorbar (as appropriate).
706
738
707
739
.. ipython :: python
740
+ :okwarning:
708
741
709
742
ds = ds.assign(w = [1 , 2 , 3 , 5 ])
710
743
@savefig ds_discrete_legend_hue_scatter.png
@@ -713,13 +746,15 @@ Additionally, the boolean kwarg ``add_guide`` can be used to prevent the display
713
746
The ``markersize `` kwarg lets you vary the point's size by variable value. You can additionally pass ``size_norm `` to control how the variable's values are mapped to point sizes.
714
747
715
748
.. ipython :: python
749
+ :okwarning:
716
750
717
751
@savefig ds_hue_size_scatter.png
718
752
ds.plot.scatter(x = " A" , y = " B" , hue = " z" , hue_style = " discrete" , markersize = " z" )
719
753
720
754
Faceting is also possible
721
755
722
756
.. ipython :: python
757
+ :okwarning:
723
758
724
759
@savefig ds_facet_scatter.png
725
760
ds.plot.scatter(x = " A" , y = " B" , col = " x" , row = " z" , hue = " w" , hue_style = " discrete" )
@@ -738,14 +773,16 @@ To follow this section you'll need to have Cartopy installed and working.
738
773
This script will plot the air temperature on a map.
739
774
740
775
.. ipython :: python
776
+ :okwarning:
741
777
742
778
import cartopy.crs as ccrs
743
779
744
780
air = xr.tutorial.open_dataset(" air_temperature" ).air
745
781
746
782
p = air.isel(time = 0 ).plot(
747
783
subplot_kws = dict (projection = ccrs.Orthographic(- 80 , 35 ), facecolor = " gray" ),
748
- transform = ccrs.PlateCarree())
784
+ transform = ccrs.PlateCarree(),
785
+ )
749
786
p.axes.set_global()
750
787
751
788
@savefig plotting_maps_cartopy.png width =100%
@@ -788,6 +825,7 @@ There are three ways to use the xarray plotting functionality:
788
825
These are provided for user convenience; they all call the same code.
789
826
790
827
.. ipython :: python
828
+ :okwarning:
791
829
792
830
import xarray.plot as xplt
793
831
@@ -837,6 +875,7 @@ think carefully about what the limits, labels, and orientation for
837
875
each of the axes should be.
838
876
839
877
.. ipython :: python
878
+ :okwarning:
840
879
841
880
@savefig plotting_example_2d_simple.png width =4in
842
881
a.plot()
@@ -857,6 +896,7 @@ xarray, but you'll have to tell the plot function to use these coordinates
857
896
instead of the default ones:
858
897
859
898
.. ipython :: python
899
+ :okwarning:
860
900
861
901
lon, lat = np.meshgrid(np.linspace(- 20 , 20 , 5 ), np.linspace(0 , 30 , 4 ))
862
902
lon += lat / 10
@@ -876,6 +916,7 @@ on a polar projection (:issue:`781`). This is why the default is to not follow
876
916
this convention when plotting on a map:
877
917
878
918
.. ipython :: python
919
+ :okwarning:
879
920
880
921
import cartopy.crs as ccrs
881
922
@@ -890,6 +931,7 @@ You can however decide to infer the cell boundaries and use the
890
931
``infer_intervals `` keyword:
891
932
892
933
.. ipython :: python
934
+ :okwarning:
893
935
894
936
ax = plt.subplot(projection = ccrs.PlateCarree())
895
937
da.plot.pcolormesh(" lon" , " lat" , ax = ax, infer_intervals = True )
@@ -908,6 +950,7 @@ You can however decide to infer the cell boundaries and use the
908
950
One can also make line plots with multidimensional coordinates. In this case, ``hue `` must be a dimension name, not a coordinate name.
909
951
910
952
.. ipython :: python
953
+ :okwarning:
911
954
912
955
f, ax = plt.subplots(2 , 1 )
913
956
da.plot.line(x = " lon" , hue = " y" , ax = ax[0 ])
0 commit comments