gboeing / osmnx

OSMnx is a Python package to easily download, model, analyze, and visualize street networks and other geospatial features from OpenStreetMap.
https://osmnx.readthedocs.io
MIT License
4.84k stars 822 forks source link

plot_graph raises an MPL error while running in a remote Linux machine #1183

Closed baygaliyev closed 3 months ago

baygaliyev commented 3 months ago

Contributing guidelines

Documentation

Existing issues

What operating system and Python version are you using?

Ubuntu/Python3.8.10

What OSMnx version are you using?

v1.4.0

Environment packages and versions

# packages in environment at /home/galiyev/miniconda3:
#
# Name                    Version                   Build  Channel
_libgcc_mutex             0.1                        main
_openmp_mutex             5.1                       1_gnu
anaconda-anon-usage       0.4.4           py312hfc0e8ea_100
archspec                  0.2.3              pyhd3eb1b0_0
boltons                   23.0.0          py312h06a4308_0
brotli-python             1.0.9           py312h6a678d5_8
bzip2                     1.0.8                h5eee18b_6
c-ares                    1.19.1               h5eee18b_0
ca-certificates           2024.3.11            h06a4308_0
certifi                   2024.2.2        py312h06a4308_0
cffi                      1.16.0          py312h5eee18b_1
charset-normalizer        2.0.4              pyhd3eb1b0_0
conda                     24.4.0          py312h06a4308_0
conda-content-trust       0.2.0           py312h06a4308_1
conda-libmamba-solver     24.1.0             pyhd3eb1b0_0
conda-package-handling    2.2.0           py312h06a4308_1
conda-package-streaming   0.9.0           py312h06a4308_0
cryptography              42.0.5          py312hdda0065_1
distro                    1.9.0           py312h06a4308_0
expat                     2.6.2                h6a678d5_0
fmt                       9.1.0                hdb19cb5_1
icu                       73.1                 h6a678d5_0
idna                      3.7             py312h06a4308_0
jsonpatch                 1.33            py312h06a4308_1
jsonpointer               2.1                pyhd3eb1b0_0
krb5                      1.20.1               h143b758_1
ld_impl_linux-64          2.38                 h1181459_1
libarchive                3.6.2                h6ac8c49_3
libcurl                   8.7.1                h251f7ec_0
libedit                   3.1.20230828         h5eee18b_0
libev                     4.33                 h7f8727e_1
libffi                    3.4.4                h6a678d5_1
libgcc-ng                 11.2.0               h1234567_1
libgomp                   11.2.0               h1234567_1
libmamba                  1.5.8                hfe524e5_2
libmambapy                1.5.8           py312h2dafd23_2
libnghttp2                1.57.0               h2d74bed_0
libsolv                   0.7.24               he621ea3_1
libssh2                   1.11.0               h251f7ec_0
libstdcxx-ng              11.2.0               h1234567_1
libuuid                   1.41.5               h5eee18b_0
libxml2                   2.10.4               hfdd30dd_2
lz4-c                     1.9.4                h6a678d5_1
menuinst                  2.0.2           py312h06a4308_1
ncurses                   6.4                  h6a678d5_0
openssl                   3.0.13               h7f8727e_1
packaging                 23.2            py312h06a4308_0
pcre2                     10.42                hebb0a14_1
pip                       24.0            py312h06a4308_0
platformdirs              3.10.0          py312h06a4308_0
pluggy                    1.0.0           py312h06a4308_1
pybind11-abi              5                    hd3eb1b0_0
pycosat                   0.6.6           py312h5eee18b_1
pycparser                 2.21               pyhd3eb1b0_0
pysocks                   1.7.1           py312h06a4308_0
python                    3.12.3               h996f2a0_1
readline                  8.2                  h5eee18b_0
reproc                    14.2.4               h6a678d5_2
reproc-cpp                14.2.4               h6a678d5_2
requests                  2.31.0          py312h06a4308_1
ruamel.yaml               0.17.21         py312h5eee18b_0
setuptools                69.5.1          py312h06a4308_0
sqlite                    3.45.3               h5eee18b_0
tk                        8.6.14               h39e8969_0
tqdm                      4.66.2          py312he106c6f_0
truststore                0.8.0           py312h06a4308_0
tzdata                    2024a                h04d1e81_0
urllib3                   2.1.0           py312h06a4308_1
wheel                     0.43.0          py312h06a4308_0
xz                        5.4.6                h5eee18b_1
yaml-cpp                  0.8.0                h6a678d5_1
zlib                      1.2.13               h5eee18b_1
zstandard                 0.22.0          py312h2c38b39_0
zstd                      1.5.5                hc292b87_2

How did you install OSMnx?

Pip

Problem description

I visualized a road network and emissions in google colab with no problem. When I tried to use the same code in a remote machine , I got an error:

Unable to init server: Could not connect: Connection refused
Unable to init server: Could not connect: Connection refused

(gaussian_dispersion.py:1176585): Gdk-CRITICAL **: 14:24:19.013: gdk_cursor_new_for_display: assertion 'GDK_IS_DISPLAY (display)' failed
DISPLAY:
MPLBACKEND: Agg
Matplotlib path: /usr/lib/python3/dist-packages/matplotlib/__init__.py
Matplotlib backend before setting: agg
Matplotlib backend after setting: agg
Matplotlib imported successfully.
OSMnx version: 1.4.0
/home/galiyev/.local/lib/python3.8/site-packages/osmnx/utils_graph.py:85: FutureWarning: You are adding a column named 'geometry' to a GeoDataFrame constructed without an active geometry column. Currently, this automatically sets the active geometry column to 'geometry' but in the future that will no longer happen. Instead, either provide geometry to the GeoDataFrame constructor (GeoDataFrame(... geometry=GeoSeries()) or use `set_geometry('geometry')` to explicitly set the active geometry column.
  gdf_edges["geometry"] = np.nan
There are 44 road segments with emission in the map
The number of emission points is 1101
Traceback (most recent call last):
  File "gaussian_dispersion.py", line 195, in <module>
    fix, ax = ox.plot_graph(road, ax=plt.gca(), node_color='none', bgcolor='w',
  File "/home/galiyev/.local/lib/python3.8/site-packages/osmnx/plot.py", line 207, in plot_graph
    _verify_mpl()
  File "/home/galiyev/.local/lib/python3.8/site-packages/osmnx/plot.py", line 977, in _verify_mpl
    raise ImportError(
ImportError: matplotlib must be installed as an optional dependency for visualization

Additional Information:

I am running this code on a remote Linux machine. DISPLAY environment variable is set to :0. Matplotlib backend is set to agg. Any insights or suggestions to resolve this issue would be greatly appreciated.

Complete minimal reproducible example

import osmnx as ox
import matplotlib.pyplot as plt

# Define the location and network type
location = "Pisa, Italy"
network_type = "drive_service"

# Fetch the road network graph from OSM
G = ox.graph_from_place(location, network_type=network_type, simplify=True)

# Plot the road network graph
fig, ax = ox.plot_graph(G, node_color="tab:blue", node_size=5, edge_color="gray", bgcolor="white")

# Add title and labels
ax.set_title("Road Network in Pisa, Italy")
ax.set_xlabel("Longitude")
ax.set_ylabel("Latitude")

# Show the plot
plt.show()
gboeing commented 3 months ago

Your error message says:

ImportError: matplotlib must be installed as an optional dependency for visualization

And as you can see in your pasted list of environment packages, you do not have matplotlib installed. It's an optional dependency of OSMnx needed only for its visualization functionality, as the error message states.

See also the installation guide: https://osmnx.readthedocs.io/en/stable/installation.html