Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pl.show() still not working (?) in interactive mode #68

Open
LucaMarconato opened this issue May 14, 2023 · 3 comments
Open

pl.show() still not working (?) in interactive mode #68

LucaMarconato opened this issue May 14, 2023 · 3 comments

Comments

@LucaMarconato
Copy link
Member

I am using the latest code from main. If I run this code as a script, the plot is shown. If I set a breakpoint (I am using PyCharm) in the line with pl.render_points()..., and then run that line in the Python interactive console, then no plot is shown and I need to call plt.show() explicitly. I think the bug was once fixed, maybe it slipped in again.

from spatialdata import SpatialData
from spatialdata.models import PointsModel, ShapesModel
from shapely import polygons, linearrings
from geopandas import GeoDataFrame
import pandas as pd
import geopandas as gpd
import numpy as np

def _make_points():
    """Helper function to make a Points element."""
    coordinates = np.array([[10, 10], [20, 20], [20, 30]], dtype=float)
    return PointsModel.parse(
        coordinates, annotation=pd.DataFrame({"genes": np.repeat("a", len(coordinates))}), feature_key="genes"
    )


def _make_squares() -> polygons:
    centroid_coordinates = np.array([[10, 10], [10, 80], [80, 20], [70, 60]])
    half_width = 6
    linear_rings = []
    for centroid in centroid_coordinates:
        min_coords = centroid - half_width
        max_coords = centroid + half_width

        linear_rings.append(
            linearrings(
                [
                    [min_coords[0], min_coords[1]],
                    [min_coords[0], max_coords[1]],
                    [max_coords[0], max_coords[1]],
                    [max_coords[0], min_coords[1]],
                ]
            )
        )
    s = polygons(linear_rings)
    polygon_series = gpd.GeoSeries(s)
    cell_polygon_table = gpd.GeoDataFrame(geometry=polygon_series)
    sd_polygons = ShapesModel.parse(cell_polygon_table)
    return sd_polygons


def _make_circles() -> GeoDataFrame:
    centroid_coordinates = np.array([[10, 10], [10, 80], [80, 20], [70, 60]])
    radius = 10
    circles = ShapesModel.parse(centroid_coordinates, geometry=0, radius=radius)
    return circles

points = _make_points()
squares = _make_squares()
circles = _make_circles()
import spatialdata_plot
sdata = SpatialData(points={'p': points}, shapes={'s': squares, 'c': circles})
sdata.pl.render_points().pl.render_shapes().pl.show()
# import matplotlib.pyplot as plt
# plt.show()
@LucaMarconato LucaMarconato changed the title pl.show() still not working (?) in interactive mode? pl.show() still not working (?) in interactive mode May 14, 2023
@LucaMarconato
Copy link
Member Author

It seems like the bug resurfaced. By running the code of this comment here (please delete the Interactive() calls) scverse/napari-spatialdata#175 (comment), in a Python console, the plots are shown only if plt.show() is called explicitly.

@LucaMarconato LucaMarconato reopened this Nov 5, 2023
@LucaMarconato
Copy link
Member Author

LucaMarconato commented Nov 5, 2023

It seems like that the check:

# Manually show plot if we're not in interactive mode
# https://stackoverflow.com/a/64523765
if not hasattr(sys, "ps1"):
    plt.show()

doesn't cover the case in which we are in Python console.

II think that the solution proposed in #71, would solve both bugs: I suggest to add a parameter show: bool = None. If ax is not None, show is set to False, otherwise it is set to True.
Then I would replace the line if not hasattr(sys, "ps1"): with if show:. Anyway I kindly ask you to double check please.

@grst
Copy link
Contributor

grst commented Feb 15, 2024

You could also look into plt.ion()/plt.ioff().

If interactive mode is activated, figures will be shown and redrawn automatically on each update. In jupyter notebooks, this is active by default.

I lean towards reducing the number of options like show =False and instead educate the users about such matplotlib options in the tutorials.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants