Giter Club home page Giter Club logo

Comments (3)

yongrenjie avatar yongrenjie commented on August 23, 2024

FWIW here's the existing code using gridspec

import penguins as pg
clipc = pg.read("./nmr/200804-6a-sc", 10, 1)

# make 2D spectrum 3x3 and projections 1x3, 3x1
gridspec_kw = {"width_ratios": [1, 3], "height_ratios": [1, 3]}
fig, axs = pg.subplots(2, 2, gridspec_kw=gridspec_kw)
# assign them to easy-to-remember variables
axblank, axf2, axf1, axmain = list(axs.flat)

# plot the 2D spectrum
clipc.stage(levels=2e5)
pg.mkplot(axmain, autolabel="nucl")

# get rid of the top-left empty space
axblank.remove()

# 1D projections
f2projp = clipc.f2projp()
f2projp.stage(color="blue")
pg.mkplot(axf2, xlabel="")
# the xlim should be set to the maximum and minimum chemical shift, i.e.
# we don't want overhang at the ends
p = f2projp.ppm_scale()
axf2.set_xlim((max(p), min(p)))
# get rid of x-axis
axf2.xaxis.set_visible(False)
axf2.spines["bottom"].set_visible(False)

# magic
from matplotlib import transforms
base_trfm = axf1.transData
rot_trfm = transforms.Affine2D().rotate_deg(90)
scale_trfm = transforms.Affine2D().scale(sx=1, sy=-1)
f1projp = clipc.f1projp()
f1projp.stage(color="blue", plot_options={"transform": scale_trfm + rot_trfm + base_trfm})
pg.mkplot(ax=axf1)
p = f1projp.ppm_scale()
axf1.set_ylim(max(p), min(p))  # this automatically inverts the y-axis
# Don't need to invert xaxis because mkplot() does it for us.
# axf1.invert_xaxis()
# However, we do need to disable the xaxis.
axf1.xaxis.set_visible(False)
axf1.spines["bottom"].set_visible(False)

# Let's put the y-axis ticks on the right.
axmain.yaxis.tick_right()

# And change the y-label position.
axmain.yaxis.label.set_rotation(0)
axmain.yaxis.label.set_horizontalalignment("left")
axmain.yaxis.label.set_verticalalignment("top")
axmain.yaxis.set_label_coords(1.02, 1)

# show the plot
pg.tight_layout()
pg.show()

Screenshot 2020-08-10 at 3 39 56 AM

from penguins.

yongrenjie avatar yongrenjie commented on August 23, 2024

Here's the code using axes_divider. This allows us to use subplots() to generate series of axes and then manipulate the individual axes after that.

fig, ax = pg.subplots(1, 1, figsize=(7, 7))

clipc.stage(levels=2e5)
pg.mkplot(ax=ax, autolabel="nucl")

### The only difference from before is that we use ax_divider instead of gridspec.
from mpl_toolkits.axes_grid1 import make_axes_locatable
ax_divider = make_axes_locatable(ax)
axf2 = ax_divider.append_axes("top", size="20%", pad="2%")
axf1 = ax_divider.append_axes("left", size="20%", pad="2%")

### Same code as before.
# 1D projections
f2projp = clipc.f2projp()
f2projp.stage(color="blue")
pg.mkplot(axf2, xlabel="")
# the xlim should be set to the maximum and minimum chemical shift, i.e.
# we don't want overhang at the ends
p = f2projp.ppm_scale()
axf2.set_xlim((max(p), min(p)))
# get rid of x-axis
axf2.xaxis.set_visible(False)
axf2.spines["bottom"].set_visible(False)

# magic
from matplotlib import transforms
base_trfm = axf1.transData
rot_trfm = transforms.Affine2D().rotate_deg(90)
scale_trfm = transforms.Affine2D().scale(sx=1, sy=-1)
f1projp = clipc.f1projp()
f1projp.stage(color="blue", plot_options={"transform": scale_trfm + rot_trfm + base_trfm})
pg.mkplot(ax=axf1)
p = f1projp.ppm_scale()
axf1.set_ylim(max(p), min(p))  # this automatically inverts the y-axis
# Don't need to invert xaxis because mkplot() does it for us.
# axf1.invert_xaxis()
# However, we do need to disable the xaxis.
axf1.xaxis.set_visible(False)
axf1.spines["bottom"].set_visible(False)

ax.yaxis.tick_right()
ax.yaxis.label.set_rotation(0)
ax.yaxis.label.set_horizontalalignment("left")
ax.yaxis.label.set_verticalalignment("top")
ax.yaxis.set_label_coords(1.02, 1)

pg.show()

Screenshot 2020-08-10 at 3 51 19 AM

from penguins.

yongrenjie avatar yongrenjie commented on August 23, 2024

There's also https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.axes.Axes.autoscale.html

set tight=True to remove the padding added on either edge of the spectrum.

from penguins.

Related Issues (19)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.