Matplotlib LTS 2.2.0中._cntr及GeoJson相关包的正确导入咨询
Hey there, let's break down your two Matplotlib 2.2.0 import questions with practical, actionable solutions:
1. Correctly importing the ._cntr module in Matplotlib LTS 2.2.0
First, a critical heads-up: ._cntr is an internal private module in Matplotlib. It’s not part of the official public API, so relying on it isn’t recommended—private modules can be modified or removed without warning in future releases, even within the LTS branch.
That said, if you have legacy code or a specific use case that requires accessing it, you can import it directly in 2.2.0 with this line:
from matplotlib import _cntr
If you’re using this for contour-related logic, I’d strongly suggest migrating to Matplotlib’s public contour API (like matplotlib.contour or matplotlib.pyplot.contour()) whenever possible. These are maintained and supported long-term, so you won’t run into unexpected breaks later.
2. Fixing GeoJson package import issues in Matplotlib 2.2.0
The fact that the package works in 2.1.0 but fails in 2.2.0 points to a change in Matplotlib’s internal structure or access rules between those versions. Here are fixes to try:
- Update the GeoJson package: First, check if there’s an updated version of the package that explicitly supports Matplotlib 2.2.0. Many maintainers patch their packages to adapt to dependency API changes, so this is the easiest fix if available.
- Adjust the package’s import logic: If updating isn’t an option, you may need to modify how the package imports Matplotlib modules. For example, if it was relying on a private module that’s now restricted or moved, replace that with a public alternative. If it was using internal contour utilities, switch to
matplotlib.contour.ContourSetor other public classes instead. - Use a module shim: If the package is looking for a Matplotlib module that’s been renamed or relocated, you can create a temporary shim before importing the GeoJson package. For example:
# Suppose the package expects matplotlib.old_module which was renamed to new_module import matplotlib.new_module as old_module import sys sys.modules['matplotlib.old_module'] = old_module # Now import your GeoJson package import your_geojson_package_name
- Switch to a standalone GeoJson library: If the package is too tightly coupled to outdated Matplotlib internals, consider using a dedicated GeoJson library (like the
geojsonPyPI package) to parse your data, then use Matplotlib’s public plotting functions (e.g.,plt.plot(),matplotlib.patches.Polygon) to visualize it. This approach is more future-proof.
内容的提问来源于stack exchange,提问作者arnaldo

