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

how to work with adjustText with multiple subplots? #58

Closed
zyxue opened this issue Jul 19, 2018 · 14 comments
Closed

how to work with adjustText with multiple subplots? #58

zyxue opened this issue Jul 19, 2018 · 14 comments

Comments

@zyxue
Copy link
Contributor

zyxue commented Jul 19, 2018

I cannot make it work well with the following code patterns

  1. adjust text for each subplot
fig, axes = plt.subplots(2, 2, figsize=(14, 12))
axes = axes.ravel()

for k, ax in enumerate(axes):
    # do some plotting
    texts = []
    # collect texts in a for loop
    # call adjust_text
  1. adjust text for texts in all subplots together
fig, axes = plt.subplots(2, 2, figsize=(14, 12))
axes = axes.ravel()

texts = []
for k, ax in enumerate(axes):
    # do some plotting
    # collect texts in a for loop

# call adjust_text

What's the right way to do it? I haven't made it work in either case yet.

@Phlya
Copy link
Owner

Phlya commented Jul 19, 2018 via email

@zyxue
Copy link
Contributor Author

zyxue commented Jul 19, 2018

Thanks, I will try it out. BTW, will ax.set_xlim or ax.set_ylim have an effect on adjustText? I saw some strange behavior before.

@Phlya
Copy link
Owner

Phlya commented Jul 19, 2018 via email

@Phlya
Copy link
Owner

Phlya commented Jul 22, 2018

@zyxue did this solve your problems?

@zyxue
Copy link
Contributor Author

zyxue commented Jul 22, 2018

@Phlya, thank you for asking, but no, it's not working yet. I am trying to get a minimal reproducible example

@zyxue
Copy link
Contributor Author

zyxue commented Jul 22, 2018

I've made an example at https://github.com/zyxue/adjustText/blob/master/figures/Examples-for-multiple-subplots.ipynb.

Also, it seems to be much slower than in a single-subplot case (17.9 ms vs 13.4 s).

@Phlya
Copy link
Owner

Phlya commented Jul 22, 2018

As I mentioned above, you have to add ax=ax to adjust_text call:

fig, axes = plt.subplots(1, 2, figsize=(8, 3), sharex=True, sharey=True)
axes = axes.ravel()

for k, ax in enumerate(axes):
    np.random.seed(0)
    x, y = np.random.random((2,30))
    ax.plot(x, y, 'bo')

    texts = []
    for i in range(len(x)):
        t = ax.text(x[i], y[i], 'Text%s' %i, ha='center', va='center')
        texts.append(t)
    adjust_text(texts, ax=ax)

image

@zyxue
Copy link
Contributor Author

zyxue commented Jul 22, 2018

Ah I see. I didn't fully get that. Ok, I will try it out later and let you know. Thank you!

@Phlya
Copy link
Owner

Phlya commented Jul 22, 2018

Concerning timing, make sure you are using axes of the same size/shape, when comparing single vs multiple subplots. If they are slightly smaller in one case, it can slow down the execution considerably.

@zyxue
Copy link
Contributor Author

zyxue commented Jul 22, 2018

I don't fully understand what you mean by "If they are slightly smaller in one case, it can slow down the execution considerably".

I have updated my notebooks, why the time spent is so different for adjusting the first and second subplots, please?

CPU times: user 11.7 s, sys: 236 ms, total: 12 s
Wall time: 12 s
CPU times: user 2.52 s, sys: 57.8 ms, total: 2.57 s
Wall time: 2.58 s

BTW, are you interested in a pull request, I thought having a such an example for multiple subplots would be helpful to other users. It's a very useful package, thank you for sharing it.

@Phlya
Copy link
Owner

Phlya commented Jul 22, 2018

Oh, I see. Interesting, I don't know why that is so different, for some reason it does a very different number of iterations each time! I'll try to investigate when I have time...
screenshot from 2018-07-23 00-51-49

@Phlya
Copy link
Owner

Phlya commented Jul 22, 2018

Yeah, would be great to add an example about this, you are not the first person to ask this question!

zyxue added a commit to zyxue/adjustText that referenced this issue Jul 22, 2018
@zyxue
Copy link
Contributor Author

zyxue commented Jul 22, 2018

ok, I've sent a pull request. Not I also removed figures/.ipynb_checkpoints from the commit as it's in your .gitignore already.

@zyxue zyxue closed this as completed Jul 24, 2018
@zyxue
Copy link
Contributor Author

zyxue commented Jul 24, 2018

Reopen because the time discrepancy for different subplots hasn't been resolsoved

Quote a previous comment:

CPU times: user 11.7 s, sys: 236 ms, total: 12 s
Wall time: 12 s
CPU times: user 2.52 s, sys: 57.8 ms, total: 2.57 s
Wall time: 2.58 s

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