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

Align labels of Bricks when subplot axes are aligned #40

Closed
W-L opened this issue Apr 26, 2023 · 5 comments
Closed

Align labels of Bricks when subplot axes are aligned #40

W-L opened this issue Apr 26, 2023 · 5 comments

Comments

@W-L
Copy link

W-L commented Apr 26, 2023

Hi ponnhide!
Big fan of your library, very happy that I came across it. I noticed that sometimes labels of Bricks are not quite aligned when the axes of subplots are aligned, but the bounding box of one of the subplots is larger. Here's a simple example:

layout_issue

import numpy as np
import pandas as pd
import plotnine as p9
import patchworklib as pw
np.random.seed(1)


data_a = np.random.normal(loc=1, scale=1, size=1000)
data_b = np.random.normal(loc=1, scale=1, size=1000)
data_c = np.random.normal(loc=1e8, scale=1, size=1000)

df = pd.DataFrame({"data_a": data_a, "data_b": data_b, "data_c": data_c})

plot0 = (p9.ggplot() + p9.geom_point(data=df, mapping=p9.aes(x='data_a', y='data_b')))
plot1 = (p9.ggplot() + p9.geom_point(data=df, mapping=p9.aes(x='data_a', y='data_c')))

plots = [plot0, plot1]
n_plots = len(plots)

m = (2, 2)
bricks = [pw.load_ggplot(p, figsize=m) for p in plots]


letters = ['A', 'B']
for i in range(n_plots):
    bricks[i].set_index(letters[i])


layout = pw.stack(bricks, operator="/")

I was wondering if there was a way to align the labels of the subplots in this case, i.e. align the A and B labels on the left?

As a workaround, I tried to use layout.get_outer_corner()[0] or layout.get_outer_corner()[0] - bricks[i].get_outer_corner()[0] to adjust the x of set_index(x=...), but was not successful. Any tips how that could be done?

Thanks very much!

@W-L
Copy link
Author

W-L commented May 15, 2023

Hi, thanks for your answer (which I can only see in my emails for some reason), and for implementing a solution.
I installed the development version using pip install git+https://github.com/ponnhide/patchworklib.git and tested the new layout.align_ylabels(), which worked for the simple example I posted above.

However, when I tried it on a slightly more complicated plot, I noticed some new issues that seemed to be caused by this version. More specifically, when using facets weird things happen to the labels of plots, even without using the new align_ylabels() method.

Here's an example:

import pandas as pd
import numpy as np
from plotnine import *
import patchworklib as pw
print(pw.__version__)

data_a = np.random.normal(loc=1, scale=1, size=1000)
data_b = np.random.normal(loc=1, scale=1, size=1000)
data_c = np.random.normal(loc=1e8, scale=1, size=1000)
data_d = np.random.choice(["a", "b", "c", "d"], size=1000)

df = pd.DataFrame({"data_a": data_a, "data_b": data_b, "data_c": data_c, "data_d": data_d})
df['data_d'] = df['data_d'].astype("category")

p0 = (ggplot()
      + geom_line(data=df, mapping=aes(y="data_b", x='data_a', color="data_d"))
      + facet_grid('~data_d', scales="free"))

p1 = (ggplot()
      + geom_line(data=df, mapping=aes(y="data_c", x='data_a', color="data_d"))
      + facet_grid('~data_d', scales="free"))

plots = [p0, p1]
bricks = [pw.load_ggplot(p) for p in plots]

letters = ['A', 'B']
for i in range(len(letters)):
    bricks[i].set_index(letters[i])

layout = pw.stack(bricks, operator="/")

This is the plot with version 0.5.2

and this is the same code with development version 0.6.1

the y-labels are duplicated and the right-most facet gets it's own x-label too.

When using align_ylabels() in version 0.6.1 with this same example, the duplication happens in the same way, but the alignment seems to work

@ponnhide
Copy link
Owner

Sorry, maybe I solved this problem. Please try same one using "pip install git+https://github.com/ponnhide/patchworklib.git"

@W-L
Copy link
Author

W-L commented May 16, 2023

Thanks! The issue of duplicated axis labels disappeared again with your update. But unfortunately the alignment also does not work in this example. The plots look the same whether layout.align_ylabels() is used or not. It does however work in the simpler example of my original comment.

@ponnhide
Copy link
Owner

Sorry, I did not consider for alining y-labels of subplots. It is difficult a little. When I have time, I try to implement this function.
By the way, I decided to quit support plotnine anymore because there were too many internal changes in plotnine v0.12.1.
I'm deeply sorry. If you wanna continue to use plotnine, please use plotnine v0.10.1.

@ponnhide
Copy link
Owner

In the GitHub version, align_ylabels and align_xlables probably work for the complex example.

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