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

Support for subplots #39

Open
mitchellvanzuijlen opened this issue Jun 15, 2022 · 2 comments
Open

Support for subplots #39

mitchellvanzuijlen opened this issue Jun 15, 2022 · 2 comments

Comments

@mitchellvanzuijlen
Copy link

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

@RemDelaporteMathurin
Copy link
Contributor

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?

@mitchellvanzuijlen
Copy link
Author

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

Thanks for the response!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants