Could you please explain to me what I should change to update my code to the new Matplotlib version?
In this code, the “isinstance(c, matplotlib.collections.LineCollection)” always returns False:
ax = plt.gca()
ax.scatter(X1[::n1], Y1[::n1], marker='.', alpha=0.1, label='XY')
for i,c in enumerate(ax.collections):
if isinstance(c, matplotlib.collections.LineCollection):
I tried some changes which do not work, I do not understand what I should write.
Sorry if my previous reply was not helpful. Here is a simple example that prints the same output with Matplotlib v3.4 and v3.9 (I do not have v3.3 installed). Before v3.5 contour plots were drawn with a LineCollection so we go through the first branch of the if-loop. Since v3.8 the ContourSet is a collection so we go through the second branch of the if-loop. Hopefully comparing the two branches will help you see how to adapt your code.
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
from matplotlib.contour import ContourSet
fig, ax = plt.subplots()
ax.contour([[0, 1], [2, 3]], levels=[1, 2])
for coll in ax.collections:
if isinstance(coll, LineCollection):
segments = coll.get_segments()
if len(segments) > 0:
print(segments[0])
elif isinstance(coll, ContourSet):
for levelseg in coll.allsegs:
if len(levelseg) > 0:
print(levelseg[0])