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 <wbittremi...@ucsd.edu <mailto:wbittremi...@ucsd.edu>> 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
    Rdkit-discuss@lists.sourceforge.net
    <mailto:Rdkit-discuss@lists.sourceforge.net>
    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


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)

# Plot molecule.
im = Draw.MolToImage(Chem.MolFromSmiles('CO\C(CC(C)C(Cl)(Cl)Cl)=C\C(=O)N(C)C(CC1=CC=CC=C1)C1=NC=CS1'))
ax.imshow(im, aspect='auto', extent=(500, 800, 14, 29))

ax.set_xlim(100, 1400)
ax.set_ylim(0, 30)

plt.savefig('plot.pdf')
plt.close()
_______________________________________________
Rdkit-discuss mailing list
Rdkit-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rdkit-discuss

Reply via email to