-1

Can't see pyplot figures when on a detached screen

Pyplot figures were visible when on attached screen and I expected its behavior to not change. It did.

Code I'm using for the plots:

    def plot(self, _):
        def total_seconds(fecha):
            return (fecha - time_limit).total_seconds()

        df = self.df.copy()
        df['fecha'] = df['fecha'].apply(to_datetime)

        dias = int(input('Días de búsqueda (Ilimitado): ') or 0)
        if dias > 0:
            today = erep_time()
            time_limit = today - timedelta(days=dias)
            busqueda = df[df['fecha'] > time_limit]
        else:
            time_limit = df['fecha'].min()
            busqueda = df

        fecha = busqueda['fecha']
        fecha_dif = busqueda['fecha'].apply(total_seconds)
        ccp_g = busqueda['ccpG']

        trend = np.poly1d(
            np.polyfit(fecha_dif, ccp_g, 10)
        )

        plt.figure(figsize=FIGSIZE)
        plt.scatter(fecha, ccp_g, color='k', marker='o')
        plt.plot(fecha, trend(fecha_dif))
        plt.xticks(rotation=90)
        plt.ylabel('cc/G')
        plt.title('Historial MM')
        plt.show()
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Vandraren
  • 1
  • 4
  • This is typical of output handling in detached screens. It's not connected actively to your system, and so the communication isn't going to be handled by any channel. You can add to you code handling for matplotlib to save an image of the plot that you can view later, see [here](https://stackoverflow.com/a/9890599/8508004). – Wayne May 27 '23 at 18:31
  • I feel like I should've known that, but TIL. That'll have to work, I guess. Thanks! – Vandraren Jun 04 '23 at 10:36

2 Answers2

0

If you are using Jupyter Notebook, I'll suggest you to include %matplotlib inline before you function. But other than that you can try different matplotlib backend such as

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
  • I'm not using Jupyter Notebook. As I said before, I'm using regular code execution on a detached screen. For more info, check the tags. – Vandraren Jun 04 '23 at 10:34
0

As Wayne pointed out, the output from matplotlib won't be handled by any channel, since it is a detached screen.

Vandraren
  • 1
  • 4
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 18 '23 at 16:08