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

use matplotlib OO API by default #122

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
use matplotlib OO API by default, avoid pyplot
  • Loading branch information
zougloub committed Sep 17, 2024
commit a2fdf24f34b2591d301b50936a62ba343ddc1a94
32 changes: 18 additions & 14 deletions brokenaxes.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from datetime import timedelta
from typing import Optional, Tuple, List

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import gridspec
from matplotlib import rcParams
from matplotlib import ticker
from matplotlib.figure import Figure
from matplotlib.artist import setp

__author__ = "Ben Dichter"

Expand Down Expand Up @@ -73,7 +73,12 @@ def __init__(
self.despine: bool = despine
self.d: float = d
self.tilt: float = tilt
self.fig: Figure = fig if fig is not None else plt.gcf()

if fig is None:
import matplotlib.pyplot as plt
fig = plt.gcf()

self.fig: Figure = fig
self.diag_handles: List = []

width_ratios = width_ratios if width_ratios is not None else self._calculate_ratios(xlims, xscale)
Expand All @@ -91,10 +96,10 @@ def __init__(
gs = gridspec.GridSpecFromSubplotSpec(
subplot_spec=subplot_spec, *args, **kwargs
)
self.big_ax = plt.Subplot(self.fig, subplot_spec)
self.big_ax = fig.add_subplot(subplot_spec)
else:
gs = gridspec.GridSpec(*args, **kwargs)
self.big_ax = plt.Subplot(self.fig, gridspec.GridSpec(1, 1)[0])
self.big_ax = fig.add_subplot(gridspec.GridSpec(1, 1)[0])

[sp.set_visible(False) for sp in self.big_ax.spines.values()]
self.big_ax.set_xticks([])
Expand All @@ -103,8 +108,7 @@ def __init__(

self.axs = []
for igs in gs:
ax = plt.Subplot(self.fig, igs)
self.fig.add_subplot(ax)
ax = fig.add_subplot(igs)
self.axs.append(ax)
self.fig.add_subplot(self.big_ax)

Expand Down Expand Up @@ -235,18 +239,18 @@ def set_spines(self):
subplotspec = ax.get_subplotspec()
if not subplotspec.is_last_row():
ax.spines["bottom"].set_visible(False)
plt.setp(ax.xaxis.get_minorticklabels(), visible=False)
plt.setp(ax.xaxis.get_minorticklines(), visible=False)
plt.setp(ax.xaxis.get_majorticklabels(), visible=False)
plt.setp(ax.xaxis.get_majorticklines(), visible=False)
setp(ax.xaxis.get_minorticklabels(), visible=False)
setp(ax.xaxis.get_minorticklines(), visible=False)
setp(ax.xaxis.get_majorticklabels(), visible=False)
setp(ax.xaxis.get_majorticklines(), visible=False)
if self.despine or not subplotspec.is_first_row():
ax.spines["top"].set_visible(False)
if not subplotspec.is_first_col():
ax.spines["left"].set_visible(False)
plt.setp(ax.yaxis.get_minorticklabels(), visible=False)
plt.setp(ax.yaxis.get_minorticklines(), visible=False)
plt.setp(ax.yaxis.get_majorticklabels(), visible=False)
plt.setp(ax.yaxis.get_majorticklines(), visible=False)
setp(ax.yaxis.get_minorticklabels(), visible=False)
setp(ax.yaxis.get_minorticklines(), visible=False)
setp(ax.yaxis.get_majorticklabels(), visible=False)
setp(ax.yaxis.get_majorticklines(), visible=False)
if self.despine or not subplotspec.is_last_col():
ax.spines["right"].set_visible(False)

Expand Down