daleroberts / itermplot

An awesome iTerm2 backend for Matplotlib, so you can plot directly in your terminal.
1.49k stars 51 forks source link

Pandas #28

Closed michaelfresco closed 6 years ago

michaelfresco commented 6 years ago

I was trying to follow along with the pandas plot tutorial, but none of the examples work.

Perhaps Pandas is not supported?

pandas visualization tutorial

michaelfresco commented 6 years ago

I actually found a solution.

When running an interactive shell, you either have to enter plt.show()

Which makes the graph show up.

Or you can use the following settings:

import matplotlib.pyplot as plt
from matplotlib import interactive
plt.hold(False)
interactive(True)
stuarteberg commented 5 years ago

Can this issue be re-opened, please? I'm curious to see if anyone knows how to make pandas plots appear automatically without explicitly calling plt.show().

As noted above, pandas plotting functions like Series.plot() and Series.hist() don't automatically trigger the plot to display. One must explicitly call plt.show(). The workaround settings given above don't seem to work any more.

Just to be explicit, here's how I'm attempting to use pandas and itermplot. Note that the plot doesn't appear until plt.show() is called.

PS -- What a lovely project!

In [2]: import os
   ...: import numpy as np
   ...: import pandas as pd
   ...: import matplotlib
   ...: import matplotlib.pyplot as plt

In [3]: os.environ['MPLBACKEND']
Out[3]: 'module://itermplot'

In [4]: os.environ['ITERMPLOT']
Out[4]: 'rv'

In [5]: matplotlib.get_backend()
Out[5]: 'module://itermplot'

In [6]: s = pd.Series(np.random.normal(size=100))

In [7]: s.hist()
Out[7]: <matplotlib.axes._subplots.AxesSubplot at 0x125c6a2b0>

In [8]: plt.show()
image

FWIW, here's my setup:

$ conda list | grep -E '^python |ipython |pandas|matplotlib|itermplot'
ipython                   7.5.0            py37h24bf2e0_0    conda-forge
itermplot                 0.331                    pypi_0    pypi
matplotlib-base           3.1.0            py37h3a684a6_1    conda-forge
pandas                    0.24.2           py37h86efe34_0    conda-forge
python                    3.7.3                h0d93f26_0    conda-forge
stuarteberg commented 3 years ago

IPython allows you to register custom display functions.

I placed the following code in ~/.ipython/profile_default/startup/enable_itermplot.py

def enable_itermplot():
    import os
    import subprocess

    try:
        # Verify that we're running within iTerm2
        r = subprocess.run('~/.iterm2/it2check', shell=True)
        assert r.returncode == 0, f"it2check returned {r.returncode}"

        # Use itermplot, with "reverse video" (dark) theme
        os.environ['MPLBACKEND'] = "module://itermplot"
        os.environ['ITERMPLOT'] = "rv"

        import matplotlib.axes
        import matplotlib.pyplot as plt

        def display_and_reset(*args):
            plt.show()
            plt.figure()  # New figure for next plot (don't re-use)

        # Tell IPython to display matplotlib figures automatically
        from IPython import get_ipython
        formatter = get_ipython().display_formatter.formatters['text/plain']
        formatter.for_type(matplotlib.axes.SubplotBase, display_and_reset)

        print("----")
        print("Enabled itermplot")

    except Exception as ex:
        print(f"Not enabling itermplot: {ex}")

enable_itermplot()
image