rgerum / pylustrator

Visualisations of data are at the core of every publication of scientific research results. They have to be as clear as possible to facilitate the communication of research. As data can have different formats and shapes, the visualisations often have to be adapted to reflect the data as well as possible. We developed Pylustrator, an interface to directly edit python generated matplotlib graphs to finalize them for publication. Therefore, subplots can be resized and dragged around by the mouse, text and annotations can be added. The changes can be saved to the initial plot file as python code.
GNU General Public License v3.0
706 stars 38 forks source link

Combining seaborn and matplotlib plots #65

Closed ngreenwald closed 9 months ago

ngreenwald commented 9 months ago

Hi, I've recently started using your package, and it's super useful! I'm wondering if there's a way to combine seaborn plots with matplotlib plots? I followed the readme, but swapping in a seaborn plotting call for one of the example plots didn't seem to work. I know in general for combining seaborn plots with base matplotlib, the best way to go is by directly referencing the ax created with fig, ax = plt.subplots(). Is there an equivalent modification that would work here?

ngreenwald commented 9 months ago

For anyone who's interested, here's what I did to get seaborn integrated. I just switched the Figure(1) syntax in the example to fig, ax = plt.subplots(), and it worked without any additional modifications.

# testing pylustrator
import matplotlib
matplotlib.use('Agg')

import matplotlib.pyplot as plt
import numpy as np

import seaborn as sns
import pandas as pd

# now import pylustrator
import pylustrator

# activate pylustrator
pylustrator.start()

# build plots as you normally would
np.random.seed(1)
t = np.arange(0.0, 2, 0.001)
y = 2 * np.sin(np.pi * t)
a, b = np.random.normal(loc=(5., 3.), scale=(2., 4.), size=(100,2)).T
b += a

fig, ax = plt.subplots(1, 3)
ax[0].plot(t, y)

ax[2].plot(a, b, "o")

# add seaborn plot
plot_df = pd.DataFrame({'class': ['a', 'a', 'a', 'b', 'b', 'b'], 'value': [1, 2, 3, 4, 5, 6]})
sns.boxplot(data=plot_df, x='class', y='value', ax=ax[1])

# show the plot in a pylustrator window
plt.show()

image