3

I'm following the tutorial here for what seems like the simplest example - a linear relationship.

I'm unclear on the very permissive imports, and don't seem to be able to show my plot.

my_seaborn_test.py:

import numpy as np
import seaborn as sns

class SeabornTest(object): 
    def run_example(self):
        sns.set(color_codes=True)
        np.random.seed(sum(map(ord, "regression")))
        tips = sns.load_dataset("tips")
        sns.lmplot(x="size", y="tip", data=tips, x_estimator=np.mean)

On the command line:

python
from my_seaborn_test import SeabornTest
st = SeabornTest()
st.run_example()

All I get is silence and this warning:

/home/me/anaconda3/envs/myenv/lib/python3.5/site-packages/matplotlib/__init__.py:892: UserWarning: axes.color_cycle is deprecated and replaced with axes.prop_cycle; please use the latter.
  warnings.warn(self.msg_depr % (key, alt_key))
tarabyte
  • 17,837
  • 15
  • 76
  • 117

1 Answers1

3

As mentioned above in the other answer, running in IPython will allow you to see it, but that isn't the only way.

The last line of your method returns a FacetGrid object, which is silently discarded after your method exits, which is why you are seeing nothing other than the warning which is produced. You will need to do something with this object (IPython automatically "prints" it when it is produced).

Change your method like so:

def run_example(self):
    sns.set(color_codes=True)
    np.random.seed(sum(map(ord, "regression")))
    tips = sns.load_dataset("tips")
    sns.lmplot(x="size", y="tip", data=tips, x_estimator=np.mean).fig.show()

Now your example will open a graphical window showing the figure

If you want to save it instead, change that last line to

sns.lmplot(x="size", y="tip", data=tips, x_estimator=np.mean).savefig("testing.png")

This sort of behavior is typical of matplotlib and thus seaborn which is built on top of matplotlib. You will have to specify what needs to be done with graphics objects (the %matplotlib inline call in IPython is actually telling IPython to catch these objects and display them).

Matthew
  • 7,440
  • 1
  • 24
  • 49