简体   繁体   中英

jupyter notebook does not show geospatial plot when running in databricks

I have a geospatial plot dataframe, and want to plot it.

I am working on a Jupyter notebook running by Databricks.

I downloaded a shapefile ( https://data.london.gov.uk/download/statistical-gis-boundary-files-london/9ba8c833-6370-4b11-abdc-314aa020d5e0/statistical-gis-boundaries-london.zip ), and manage to subset only a section of London using:

import geopandas as gpd
import descartes
import pandas as pd
import matplotlib.pyplot as plt
fp = '/dbfs/FileStore/tables/LondonShapeFile/OA_2011_London_gen_MHW.shp'
map_df = gpd.read_file(fp, encoding="utf-8")
orp = map_df[map_df['WD11NM_BF']=='Orpington']
print(orp.shape)
orp.plot()

I get:

(50, 18)
Out[95]: <matplotlib.axes._subplots.AxesSubplot at 0x7f064e8df5c0>

I did not get a plot, so tried:

%matplotlib inline

but got:

%matplotlib inline is not supported in Databricks.
You can display matplotlib figures using display(). For an example, see https://docs.databricks.com/user-guide/visualizations/matplotlib-and-ggplot.html

Following the suggested example on https://docs.databricks.com/user-guide/visualizations/matplotlib-and-ggplot.html works,

import numpy as np
df3 = pd.DataFrame(np.random.randn(1000, 2), columns=['B', 'C']).cumsum()
df3['A'] = pd.Series(list(range(len(df3))))
dd=df3.plot(x='A', y='B')
display(dd.figure)

but when I try to implement something similar with the geopandas df, I get multiple errors:

orp.dipslay()
AttributeError: 'GeoDataFrame' object has no attribute 'dipslay'
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<command-797544454504214> in <module>()
      1 import descartes
----> 2 orp.dipslay()

/databricks/python/lib/python3.5/site-packages/pandas/core/generic.py in __getattr__(self, name)
   2742             if name in self._info_axis:
   2743                 return self[name]
-> 2744             return object.__getattribute__(self, name)
   2745 
   2746     def __setattr__(self, name, value):

AttributeError: 'GeoDataFrame' object has no attribute 'dipslay'
#
display(orp.plot())
/databricks/python/lib/python3.5/site-packages/matplotlib/pyplot.py:524: RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (`matplotlib.pyplot.figure`) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam `figure.max_open_warning`).
  max_open_warning, RuntimeWarning)
Exception: Cannot call display(<class 'matplotlib.axes._subplots.AxesSubplot'>)
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
<command-797544454504291> in <module>()
----> 1 display(orp.plot())

/local_disk0/tmp/1553768511027-0/PythonShell.py in display(self, input, *args, **kwargs)
    860             input.help() # This is going to display the help as a side-effect
    861         else:
--> 862             raise Exception(genericErrorMsg)
    863 
    864     def displayHTML(self, html):

Exception: Cannot call display(<class 'matplotlib.axes._subplots.AxesSubplot'>)
 Call help(display) for more info.

and

 display(orp.plot()) /databricks/python/lib/python3.5/site-packages/matplotlib/pyplot.py:524: RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (`matplotlib.pyplot.figure`) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam `figure.max_open_warning`). max_open_warning, RuntimeWarning) Exception: Cannot call display(<class 'matplotlib.axes._subplots.AxesSubplot'>) --------------------------------------------------------------------------- Exception Traceback (most recent call last) <command-797544454504291> in <module>() ----> 1 display(orp.plot()) /local_disk0/tmp/1553768511027-0/PythonShell.py in display(self, input, *args, **kwargs) 860 input.help() # This is going to display the help as a side-effect 861 else: --> 862 raise Exception(genericErrorMsg) 863 864 def displayHTML(self, html): Exception: Cannot call display(<class 'matplotlib.axes._subplots.AxesSubplot'>) Call help(display) for more info. 

Something is not working in DataBricks so I followed below steps and run it on my laptop (Windows 10) and it works.

Windows 10 installation of geopandas
1) I go this website to download the following installation files (.whl):
    https://www.lfd.uci.edu/~gohlke/pythonlibs/
        1.1) Shapely
        1.2) GDAL and
        1.3) Fiona
2) Go to the download file and run in anaconda prompt
     pip install < *.whl>  for Shapely, GDAL and Fiona

在此输入图像描述

This solution worked in the end

fp = '/dbfs/FileStore/tables/LondonShapeFile/OA_2011_London_gen_MHW.shp'
map_df = gpd.read_file(fp, encoding="utf-8")
print(map_df.shape)
ldn = map_df.plot()
display(ldn.figure)

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM