Hi Wout,
I couldn't figure out how to get ax.imshow to do anything sensible (the way
extents work is completely opaque to me), but I was able to get something
that isn't completely terrible using ax.figure.figimage.
An adapted version of your script (that also uses the new drawing code) is
attached.
-greg
On Fri, Aug 9, 2019 at 7:42 PM Wout Bittremieux <[email protected]>
wrote:
> Hi Greg,
>
> Please see the attachment for the basic code I use to plot spectra and
> molecules. It'd be great if I can somehow improve the quality of the
> molecule!
>
> @Dima: I'm not using any SVG functionality because matplotlib can't
> handle that. So I don't think doing the SVG transform is directly
> possible in matplotlib.
> Alternatively I could export both the spectrum plot and the molecule to
> SVG files and then combine them afterwards. But in that case it's not
> possible to manipulate both elements in a single matplotlib figure.
>
> Best,
> Wout
>
> On 07/08/2019 21:44, Greg Landrum wrote:
> > That's an interesting question. I think there probably are some ways of
> > tweaking this and improving things, but I don't have a convenient way to
> > test. If you can share the python (+data) you used to construct that PDF
> > I will see if I can come up with a solution.
> >
> > -greg
> >
> >
> > On Thu, Aug 8, 2019 at 2:23 AM Wout Bittremieux <[email protected]
> > <mailto:[email protected]>> wrote:
> >
> > Dear RDKit team,
> >
> > I'm trying to draw a molecule on a spectrum plot from Matplotlib
> using
> > MolToImage. This is some abbreviated code:
> >
> > ```
> > import matplotlib.pyplot as plt
> > from rdkit import Chem
> > from rdkit.Chem import Draw
> >
> > fig, ax = plt.subplots()
> >
> > ax.plot(...)
> >
> > im = Draw.MolToImage(Chem.MolFromSmiles(smiles))
> > ax.imshow(im, aspect='auto', extent=(x, x+width, y, y + height))
> >
> > plt.savefig('fig.pdf')
> > plt.close()
> > ```
> >
> > Unfortunately the quality of the molecule drawing is rather poor (see
> > attachment; nonsensical spectrum and molecule). This seems to be true
> > for non-SVG drawing in general, and unfortunately it's not really
> > possible to combine SVG output with Matplotlib functionality.
> >
> > Is there any way I can improve the quality of the MolToImage output?
> > I've tried to change some of the DrawingOptions, but the result
> always
> > remains very pixelated and low quality.
> >
> > Thank you,
> > Wout
> > _______________________________________________
> > Rdkit-discuss mailing list
> > [email protected]
> > <mailto:[email protected]>
> > https://lists.sourceforge.net/lists/listinfo/rdkit-discuss
> >
>
import matplotlib.pyplot as plt
import numpy as np
from rdkit import Chem
from rdkit.Chem import Draw
from rdkit.Chem.Draw import rdMolDraw2D
from io import BytesIO
np.random.seed(2)
num_peaks = 50
mz = np.random.uniform(100, 1400, num_peaks)
intensity = np.random.lognormal(0, 2, num_peaks)
intensity[intensity < 0] = 0
fig, ax = plt.subplots(figsize=(9, 6))
# Plot peaks.
for mz, intensity in zip(mz, intensity):
ax.plot([mz, mz], [0, intensity], c='black', zorder=10)
img_size = fig.get_size_inches()*fig.dpi
# Plot molecule.
m = Chem.MolFromSmiles('CO\C(CC(C)C(Cl)(Cl)Cl)=C\C(=O)N(C)C(CC1=CC=CC=C1)C1=NC=CS1')
# draw the molecule with the new drawing code:
d2d = Draw.MolDraw2DCairo(int(img_size[0]*0.2),int(img_size[1]*0.2))
d2d.drawOptions().clearBackground=False
Draw.PrepareAndDrawMolecule(d2d,m)
d2d.FinishDrawing()
png = d2d.GetDrawingText()
bio = BytesIO(png)
im = plt.imread(bio)
ax.set_xlim(100, 1400)
ax.set_ylim(0, 30)
ax.figure.figimage(im, img_size[0]*0.4, img_size[1]*0.6, alpha=.85, zorder=1)
plt.savefig('blah.pdf')
plt.close()
_______________________________________________
Rdkit-discuss mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/rdkit-discuss