ahuang11's picture
Upload 52 files
b9a0f21 verified
|
raw
history blame
3.08 kB
## Subplots
When plotting multiple columns, hvPlot will overlay the plots onto one axis by default so that they can be compared easily in a compact format:
```python
import xarray as xr
import hvplot.pandas # noqa
import hvplot.xarray # noqa
from hvplot.sample_data import airline_flights, us_crime
us_crime.hvplot(x='Year', y=['Burglary rate', 'Violent Crime rate', 'Robbery rate'], value_label='Rate')
```
If you wish, you can instead set `subplots=True` to split each column into its own separate plot:
```python
us_crime.hvplot(x='Year', y=['Burglary rate', 'Violent Crime rate', 'Robbery rate'],
value_label='Rate', subplots=True, width=300, height=200)
```
By default, the subplots will have linked, normalized axes, to facilitate comparing the numerical values across plots (try panning or zooming in any one of the plots, to see how the others change.)
However, if the data covers widely different numerical ranges, you can specify `shared_axes=False` to give each plot its own range:
```python
us_crime.hvplot(x='Year', y=['Robbery', 'Robbery rate', 'Burglary', 'Burglary rate'],
width=350, height=300, subplots=True, shared_axes=False).cols(2)
```
(Notice the very different y axis ranges between the plots.) Here we also specified `.cols(2)` to allow up to two plots per line, wrapping the rest onto subsequent rows.
You can use the `subplots=True` (and `shared_axes` if desired) arguments when using the ``by`` keyword as well, if you want to group the data along a dimension:
```python
flights = airline_flights.read()
flight_subset = flights[flights.carrier.isin(['OH', 'F9', 'US'])].sample(2000)
flight_subset.hvplot.scatter(x='arrdelay', y='depdelay', by='carrier',
subplots=True, width=250, height=250, alpha=0.1)
```
## Grids
`subplot=True` lays out plots sequentially, breaking lines if requested with the `.cols()` method but otherwise formatting each plot independently. You can instead arrange multidimensional data into an explicit 1D row of plots or a 2D grid of plots with shared axes, to allow easier comparisons across large numbers of plots. To make a row or grid plot, just specify the ``col`` keyword and add a ``row`` keyword if you want a grid:
```python
flight_subset.sort_values('dayofweek').hvplot.scatter(x='arrdelay', y='depdelay',
row='dayofweek', col='carrier', alpha=0.2)
```
(Just declaring `row` to get a single column is not currently supported.) Here you can see that compared to the subplot versions above, the axis ticks and labels are shared to save space and make comparisons easier.
If you do not require an x axis and y axis for each plot at all, you can disable it with the ``xaxis`` and ``yaxis`` options:
```python
air_ds = xr.tutorial.open_dataset('air_temperature').load()
air_ds.air.isel(time=slice(0, 5)).hvplot(col='time', xaxis=False, yaxis=False, colorbar=False)
```
Using subplots and grids in this way is supported throughout the hvPlot API, making it simple to determine how you want your data laid out and overlaid.