Skip to content

Improve Demo Text Rotation Mode #13612

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

Merged
merged 1 commit into from
Mar 10, 2019

Conversation

timhoffm
Copy link
Member

@timhoffm timhoffm commented Mar 7, 2019

PR Summary

  • Rewrote description.
  • Improved layout, giving more visual focus on the relation between the label and the reference point.

Old example

New example

@timhoffm timhoffm force-pushed the example-text-rotation-mode branch from 5a583a4 to 5d7a515 Compare March 7, 2019 01:00
@ImportanceOfBeingErnest
Copy link
Member

ImportanceOfBeingErnest commented Mar 7, 2019

I think I liked the old design better, mainly because each case got its own carré, so the alignment is easier to grasp.

Also I think showing the bounding box in case of the "default" rotation would make the concept clearer.

image

Code to produce the above (quickly tinkered from the original example - not meant to be taken over literally):
from mpl_toolkits.axes_grid1.axes_grid import ImageGrid


def test_rotation_mode(fig, mode, subplot_location):
    ha_list = "left center right".split()
    va_list = "top center baseline bottom".split()
    grid = ImageGrid(fig, subplot_location,
                     nrows_ncols=(len(va_list), len(ha_list)),
                     share_all=True, aspect=True,
                     # label_mode='1',
                     cbar_mode=None)

    for ha, ax in zip(ha_list, grid.axes_row[-1]):
        ax.axis["bottom"].label.set_text(ha)

    # create a grid of axes to display text on.
    grid.axes_row[0][1].set_title(mode, size="large")

    for va, ax in zip(va_list, grid.axes_column[0]):
        ax.axis["left"].label.set_text(va)

    # use a different horizontal and vertical alignment for the text in each
    # axes.
    
    if mode=="default":
        kw = dict()
    else:
        kw = dict(bbox=dict(boxstyle="square,pad=0.", ec="none", fc="0.4", alpha=0.4))
    
    i = 0
    for va in va_list:
        for ha in ha_list:
            ax = grid[i]
            for axis in ax.axis.values():
                axis.toggle(ticks=False, ticklabels=False)

            # add text to the axes. Set the rotation_mode, horizontal
            # alignment (ha) and vertical alignment (va).
            tx = ax.text(0.5, 0.5, "Tpg",
                    size="large", rotation=40,
                    ha=ha, va=va,
                    rotation_mode=mode, **kw)
            ax.axvline(0.5, color="skyblue", zorder=0)
            ax.axhline(0.5, color="skyblue", zorder=0)
            ax.plot(0.5, 0.5, color="C0", marker="o", zorder=1)
            ax.axis([0,1,0,1])
            
            if mode=="default":
                fig.canvas.draw()
                bb=tx.get_window_extent().inverse_transformed(ax.transData)
                rect = plt.Rectangle((bb.x0, bb.y0), bb.width, bb.height,
                                     facecolor="0.4", alpha=0.4, zorder=2)
                ax.add_patch(rect)
            
            i += 1


if 1:
    import matplotlib.pyplot as plt
    fig = plt.figure(1, figsize=(8, 6))
    fig.clf()

    test_rotation_mode(fig, "default", 121)
    test_rotation_mode(fig, "anchor", 122)
    plt.show()

...this is hidden, collapsable content...

@jklymak jklymak added this to the v3.1.0 milestone Mar 7, 2019
@timhoffm
Copy link
Member Author

timhoffm commented Mar 7, 2019

Thanks, showing the bbox is a good idea I've taken over that approach.

@timhoffm timhoffm force-pushed the example-text-rotation-mode branch from 5d7a515 to d7aaab0 Compare March 7, 2019 06:56
@ImportanceOfBeingErnest
Copy link
Member

ImportanceOfBeingErnest commented Mar 7, 2019

Drawing the figure multiple times is probably inefficient. Below the example it says 2 seconds, which seems a lot. Maybe one can externalize the part where the rectangles are drawn, such that figure only needs to be drawn once, like

    # use a different text alignment in each axes
    texts = []
    for (va, ha), ax in zip([(x, y) for x in va_list for y in ha_list], grid):
            # prepare axes layout
            for axis in ax.axis.values():
                axis.toggle(ticks=False, ticklabels=False)
            ax.axis([0, 1, 0, 1])
            ax.axvline(0.5, color="skyblue", zorder=0)
            ax.axhline(0.5, color="skyblue", zorder=0)
            ax.plot(0.5, 0.5, color="C0", marker="o", zorder=1)

            # add text with rotation and alignment settings
            tx = ax.text(0.5, 0.5, "Tpg",
                         size="x-large", rotation=40,
                         horizontalalignment=ha, verticalalignment=va,
                         rotation_mode=mode, **kw)
            texts.append(tx)

    if mode == "default":
        # highlight bbox
        fig.canvas.draw()
        for ax, tx in zip(grid,texts):
            bb = tx.get_window_extent().inverse_transformed(ax.transData)
            rect = plt.Rectangle((bb.x0, bb.y0), bb.width, bb.height,
                                 facecolor="C1", alpha=0.3, zorder=2)
            ax.add_patch(rect)

@timhoffm timhoffm force-pushed the example-text-rotation-mode branch from d7aaab0 to e1c3c5d Compare March 7, 2019 21:58
@timhoffm
Copy link
Member Author

timhoffm commented Mar 7, 2019

Only drawing once saves about 25% of the time. I guess a significant part of the time goes into creating the axes. Anyway, the code is better, so I've applied it. Thanks.

@QuLogic
Copy link
Member

QuLogic commented Mar 8, 2019

Looking at the gallery for this, I also found this example; should we merge them together? I like the new look here, but the other one has some different textual explanation as well.

@timhoffm
Copy link
Member Author

timhoffm commented Mar 9, 2019

I’ve seen the other rotation example as well. In principle it would make sense to merge them. However`, I‘ve decided not do it as part of this PR to keep the the changes more managable. Sorting out styling was the main focus of this PR. I‘m happy to do a follow-up.

@NelleV NelleV merged commit 6379f07 into matplotlib:master Mar 10, 2019
@NelleV
Copy link
Member

NelleV commented Mar 10, 2019

Thanks @timhoffm !

meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Mar 10, 2019
jklymak added a commit that referenced this pull request Mar 10, 2019
…612-on-v3.1.x

Backport PR #13612 on branch v3.1.x (Improve Demo Text Rotation Mode)
@timhoffm timhoffm deleted the example-text-rotation-mode branch March 11, 2019 06:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants