nschloe / matplotx

:bar_chart: More styles and useful extensions for Matplotlib

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for subplots

mitchellvanzuijlen opened this issue · comments

Perhaps this is already implemented and I'm just unable to find it. I think this package in general is great; very easy to use and very beautiful. Thank you for your time making it.

I'm unable to get matplotx working properly when using subplots. Adapting the Clean line plots (dufte) example to include two subplots (side-by-side, or one-below-the-other) appears not to work.

import matplotlib.pyplot as plt
import matplotx
import numpy as np

# create data
rng = np.random.default_rng(0)
offsets = [1.0, 1.50, 1.60]
labels = ["no balancing", "CRV-27", "CRV-27*"]
x0 = np.linspace(0.0, 3.0, 100)
y = [offset * x0 / (x0 + 1) + 0.1 * rng.random(len(x0)) for offset in offsets]

fig, axes = plt.subplots(2,1)                                           # add subplots

for ax in axes:                                                         # Let's make two identical subplots
    with plt.style.context(matplotx.styles.dufte):
        for yy, label in zip(y, labels):
            ax.plot(x0, yy, label=label)                                # changed plt. to ax.
        ax.set_xlabel("distance [m]")                                   # changed plt. to ax.
        matplotx.ylabel_top("voltage [V]")                              # move ylabel to the top, rotate
        matplotx.line_labels()                                          # line labels to the right
        #plt.show()                                                     # Including this adds the 'pretty axis' below the subplots.                             

image

Hi,
You have two options:

  1. Set the current active axis:
import matplotlib.pyplot as plt
import matplotx
import numpy as np

# create data
rng = np.random.default_rng(0)
offsets = [1.0, 1.50, 1.60]
labels = ["no balancing", "CRV-27", "CRV-27*"]
x0 = np.linspace(0.0, 3.0, 100)
y = [offset * x0 / (x0 + 1) + 0.1 * rng.random(len(x0)) for offset in offsets]

fig, axes = plt.subplots(2, 1)  # add subplots

for ax in axes:  # Let's make two identical subplots
    plt.sca(ax)
    with plt.style.context(matplotx.styles.dufte):
        for yy, label in zip(y, labels):
            ax.plot(x0, yy, label=label)  # changed plt. to ax.
        ax.set_xlabel("distance [m]")  # changed plt. to ax.
        matplotx.ylabel_top("voltage [V]")  # move ylabel to the top, rotate
        matplotx.line_labels()  # line labels to the right
plt.show()

Produces
image

  1. pass the current axis to line_labels() (⚠️ doesn't work!)
import matplotlib.pyplot as plt
import matplotx
import numpy as np

# create data
rng = np.random.default_rng(0)
offsets = [1.0, 1.50, 1.60]
labels = ["no balancing", "CRV-27", "CRV-27*"]
x0 = np.linspace(0.0, 3.0, 100)
y = [offset * x0 / (x0 + 1) + 0.1 * rng.random(len(x0)) for offset in offsets]

fig, axes = plt.subplots(2, 1)  # add subplots

for ax in axes:  # Let's make two identical subplots
    with plt.style.context(matplotx.styles.dufte):
        for yy, label in zip(y, labels):
            ax.plot(x0, yy, label=label)  # changed plt. to ax.
        ax.set_xlabel("distance [m]")  # changed plt. to ax.
        matplotx.ylabel_top("voltage [V]")  # move ylabel to the top, rotate
        matplotx.line_labels(ax)  # line labels to the right
plt.show()

For some reason option 2 doesn't work. @nschloe any idea?

Hi, please see the Pull Request that I submitted for this.

Thanks for the response!