0

I trying the following code to plot some hvplots over each other.

feat_prep=rivers.hvplot(width=600,height=500,color='red')*\
    roads.hvplot(width=600,height=500,color='yellow')*\
    borders.hvplot(width=600,height=500,color='orange')*\
    cities_gdf.hvplot(width=600,height=500,color='magenta',hover=True)*\
cities_gdf.hvplot.labels(x='lon',y='lat',text='city',width=600,height=500,text_baseline='bottom',hover=False,fontsize=15,text_color='white')

a=ipw.IntSlider(description='level',min=0,max=29,continuous_update=False)
def f1(i):
    # Uses previously prepared features, but it doesn't seem to reduce rendering time
    display(r.paw.interactive.isel(lev=i).hvplot(cmap='viridis_r',xlabel='lon (Degrees East)',ylabel='lat (Degrees North)',width=600,height=500)*\
    feat_prep
         )

out = ipw.interactive_output(f1, {'i': a})
out.layout={'height':'550px'}
ipw.VBox([a, out])

rivers, roads, borders, and cities_gdf are generated as follows:

r=xr.open_dataset('/f/work/julich/dg-rr/analytcis/plant_available_water/results/2021-09-30T00:00:00/2442-07-11_00:00:00.nc') # Open the netCDF file as dataset instead of dataarray as some weidgest (e.g., panel slider) needs a named dataarray which still produces some errors
r=r.rename({'__xarray_dataarray_variable__':'paw'})

# Read shape files for different features in the specified coordinates
river_path="/f/work/julich/dg-rr/analytcis/plant_available_water/sample_visualization/shp_data/rivers_OSM/rivers_OSM.shp"
roads_path="/f/work/julich/dg-rr/analytcis/plant_available_water/sample_visualization/shp_data/roads_OSM/roads_OSM.shp"
borders_path="/f/work/julich/dg-rr/analytcis/plant_available_water/sample_visualization/shp_data/borders_OSM/borders_OSM.shp"

rivers=gpd.read_file(river_path) # Read rivers features
roads=gpd.read_file(roads_path) # Read roads features
borders=gpd.read_file(borders_path) # Read borders features

# Create a bounding box for required coordinates
b=box(r['lon'].min().item(),r['lat'].min().item(),r['lon'].max().item(),r['lat'].max().item())
b_gdf=gpd.GeoDataFrame([1],geometry=[b],crs=rivers.crs)

# Clip feature to specified input data box
rivers=rivers.clip(b_gdf)
roads=roads.clip(b_gdf)
borders=borders.clip(b_gdf)

# Create a geodataframe of required cities
cities_df=pd.DataFrame({'city':['Jülich','Köln','Bonn','Düsseldorf','Koblenz','Bitburg','Maastricht','Aachen','Nürburg','Malmedy','Lüttich'],\
                    'lon':[6.371537,6.952778,7.109601,6.777019,7.594615,6.521794,5.691512,6.084142,6.978922,6.029222,5.575556],\
                    'lat':[50.916966,50.936389,50.727647,51.222089,50.351882,49.964940,50.844460,50.771486,50.344968,50.421748,50.645278]})
cities_gdf=gpd.GeoDataFrame(cities_df,geometry=gpd.points_from_xy(cities_df.lon, cities_df.lat))

The final out takes a long time to be drawn. Also, changing the Intslider value takes a long time to re-draw the plot. I tried different options like "rasterize" and "datashade" but still taking a long time. I wonder how to reduce the plotting time?

Thanks

shambakey1
  • 37
  • 7
  • Related question please: Is it possible to fix the rivers, roads, borders and cities plots and only update the xarray data according to slider. Thus, all data other than "r" will act as a fixed layer, while the xarray data is the updated layer? but the fixed layer should be drawn on top of the "r" updated layer. Otherwise, the features in the fixed layer will not be shown. – shambakey1 Jan 15 '22 at 10:01
  • Hi, it's difficult to help you without the actual data and without knowing what you mean by slow. One thing to look into is that you may need to project your data to the right projection beforehand, to avoid having hvplot recompute the projection everytime it needs the projected data. – MaximeL Feb 15 '22 at 11:44

0 Answers0