Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to hide or show tick labels. #6018

Merged
merged 2 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
98 changes: 66 additions & 32 deletions networkx/drawing/nx_pylab.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@ def draw_networkx(G, pos=None, arrows=None, with_labels=True, **kwds):
label : string, optional
Label for graph legend

hide_ticks : bool, optional
Hide ticks of axes. When `True` (the default), ticks and ticklabels
are removed from the axes. To set ticks and tick labels to the pyplot default,
use ``hide_ticks=False``.

kwds : optional keywords
See networkx.draw_networkx_nodes(), networkx.draw_networkx_edges(), and
networkx.draw_networkx_labels() for a description of optional keywords.
Expand Down Expand Up @@ -326,6 +331,7 @@ def draw_networkx_nodes(
edgecolors=None,
label=None,
margins=None,
hide_ticks=True,
):
"""Draw the nodes of the graph G.

Expand Down Expand Up @@ -390,6 +396,11 @@ def draw_networkx_nodes(
be in the range ``[0, 1]``. See :meth:`matplotlib.axes.Axes.margins`
for details. The default is `None`, which uses the Matplotlib default.

hide_ticks : bool, optional
Hide ticks of axes. When `True` (the default), ticks and ticklabels
are removed from the axes. To set ticks and tick labels to the pyplot default,
use ``hide_ticks=False``.

Returns
-------
matplotlib.collections.PathCollection
Expand Down Expand Up @@ -450,14 +461,15 @@ def draw_networkx_nodes(
edgecolors=edgecolors,
label=label,
)
ax.tick_params(
axis="both",
which="both",
bottom=False,
left=False,
labelbottom=False,
labelleft=False,
)
if hide_ticks:
ax.tick_params(
axis="both",
which="both",
bottom=False,
left=False,
labelbottom=False,
labelleft=False,
)

if margins is not None:
if isinstance(margins, Iterable):
Expand Down Expand Up @@ -682,6 +694,7 @@ def draw_networkx_edges(
connectionstyle="arc3",
min_source_margin=0,
min_target_margin=0,
hide_ticks=True,
):
r"""Draw the edges of the graph G.

Expand Down Expand Up @@ -781,6 +794,11 @@ def draw_networkx_edges(
min_target_margin : int (default=0)
The minimum margin (gap) at the end of the edge at the target.

hide_ticks : bool, optional
Hide ticks of axes. When `True` (the default), ticks and ticklabels
are removed from the axes. To set ticks and tick labels to the pyplot default,
use ``hide_ticks=False``.

Returns
-------
matplotlib.collections.LineCollection or a list of matplotlib.patches.FancyArrowPatch
Expand Down Expand Up @@ -1013,14 +1031,16 @@ def draw_networkx_edges(
corners = (minx - padx, miny - pady), (maxx + padx, maxy + pady)
ax.update_datalim(corners)
ax.autoscale_view()
ax.tick_params(
axis="both",
which="both",
bottom=False,
left=False,
labelbottom=False,
labelleft=False,
)

if hide_ticks:
ax.tick_params(
axis="both",
which="both",
bottom=False,
left=False,
labelbottom=False,
labelleft=False,
)

return edge_viz_obj

Expand All @@ -1039,6 +1059,7 @@ def draw_networkx_labels(
verticalalignment="center",
ax=None,
clip_on=True,
hide_ticks=True,
):
"""Draw node labels on the graph G.

Expand Down Expand Up @@ -1087,6 +1108,11 @@ def draw_networkx_labels(
clip_on : bool (default=True)
Turn on clipping of node labels at axis boundaries

hide_ticks : bool, optional
Hide ticks of axes. When `True` (the default), ticks and ticklabels
are removed from the axes. To set ticks and tick labels to the pyplot default,
use ``hide_ticks=False``.

Returns
-------
dict
Expand Down Expand Up @@ -1138,14 +1164,15 @@ def draw_networkx_labels(
)
text_items[n] = t

ax.tick_params(
axis="both",
which="both",
bottom=False,
left=False,
labelbottom=False,
labelleft=False,
)
if hide_ticks:
ax.tick_params(
axis="both",
which="both",
bottom=False,
left=False,
labelbottom=False,
labelleft=False,
)

return text_items

Expand All @@ -1169,6 +1196,7 @@ def draw_networkx_edge_labels(
node_size=300,
nodelist=None,
connectionstyle="arc3",
hide_ticks=True,
):
"""Draw edge labels.

Expand Down Expand Up @@ -1236,6 +1264,11 @@ def draw_networkx_edge_labels(
`matplotlib.patches.FancyArrowPatch` for more info.
If Iterable, index indicates i'th edge key of MultiGraph

hide_ticks : bool, optional
Hide ticks of axes. When `True` (the default), ticks and ticklabels
are removed from the axes. To set ticks and tick labels to the pyplot default,
use ``hide_ticks=False``.

Returns
-------
dict
Expand Down Expand Up @@ -1456,14 +1489,15 @@ def draw(self, renderer):
ax=ax,
)

ax.tick_params(
axis="both",
which="both",
bottom=False,
left=False,
labelbottom=False,
labelleft=False,
)
if hide_ticks:
ax.tick_params(
axis="both",
which="both",
bottom=False,
left=False,
labelbottom=False,
labelleft=False,
)

return text_items

Expand Down
22 changes: 22 additions & 0 deletions networkx/drawing/tests/test_pylab.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,3 +845,25 @@ def test_no_warning_on_default_draw_arrowstyle(draw_fn):
assert len(w) == 0

plt.delaxes(ax)


@pytest.mark.parametrize("hide_ticks", [False, True])
@pytest.mark.parametrize(
"method",
[
nx.draw_networkx,
nx.draw_networkx_edge_labels,
nx.draw_networkx_edges,
nx.draw_networkx_labels,
nx.draw_networkx_nodes,
],
)
def test_hide_ticks(method, hide_ticks):
G = nx.path_graph(3)
pos = {n: (n, n) for n in G.nodes}
_, ax = plt.subplots()
method(G, pos=pos, ax=ax, hide_ticks=hide_ticks)
for axis in [ax.xaxis, ax.yaxis]:
assert bool(axis.get_ticklabels()) != hide_ticks

plt.delaxes(ax)