paulbrodersen / netgraph

Publication-quality network visualisations in python
GNU General Public License v3.0
660 stars 39 forks source link

Fix "AttributeError: module 'matplotlib.axes' has no attribute '_subplots'" #61

Closed speedsmith closed 1 year ago

speedsmith commented 1 year ago

The _subplots class seems to have been removed from matplotlib version 3.7 or earlier. The change made here to _main.py avoids an error that crashes the original author's demo code (below) from the Quickstart under the comment "# Netgraph uses matplotlib for creating the visualisation." Running this demo code with the original _main.py caused the error:

AttributeError: module 'matplotlib.axes' has no attribute '_subplots'

Minimum Reproducible Example:

import matplotlib.pyplot as plt
from netgraph import Graph, InteractiveGraph, EditableGraph

# Several graph formats are supported:

# Netgraph uses matplotlib for creating the visualisation.
# Node and edge artistis are derived from `matplotlib.patches.PathPatch`.
# Node and edge labels are `matplotlib.text.Text` instances.
# Standard matplotlib syntax applies.
fig, ax = plt.subplots(figsize=(5, 4))
plot_instance = Graph([(0, 1)], node_labels=True, edge_labels=True, ax=ax)
plot_instance.node_artists[0].set_alpha(0.2)
plot_instance.edge_artists[(0, 1)].set_facecolor('red')
plot_instance.edge_label_artists[(0, 1)].set_style('italic')
plot_instance.node_label_artists[1].set_size(10)
ax.set_title("This is my fancy title.")
ax.set_facecolor('honeydew') # change background color
fig.canvas.draw()  # force redraw to display changes
fig.savefig('test.pdf', dpi=300)
plt.show()
paulbrodersen commented 1 year ago

Thank you, that is an incredibly important bugfix. I am on matplotlib 3.4, so they must have indeed changed that in the last or one of the last updates, even though the changelogs don't mention it. Have a great day!

paulbrodersen commented 1 year ago

I just pushed a new version to PyPI so these changes should now be live.