Giter Club home page Giter Club logo

Comments (2)

jklymak avatar jklymak commented on July 28, 2024 1

This seems useful, but hard to generalize. I am skeptical that it would be made default part of Matplotlib proper. We have a pretty straightforward GUI event handler that would be just a few lines of code to make something like this work:

import matplotlib.pyplot as plt
import numpy as np

def on_click(event):
    for i, ax in enumerate(axs.flat):
        if event.inaxes == ax:
            fig, new_ax = plt.subplots()
            new_ax.imshow(images[i], cmap='gray')
            new_ax.set_title(f'Image {i+1}')
            new_ax.axis('off')
            fig.show()

def create_random_image():
    return np.random.rand(100, 100)

# Generate four random images
images = [create_random_image() for _ in range(4)]

# Create a main figure to display the images
main_fig, axs = plt.subplots(2, 2, layout='compressed')

for i, ax in enumerate(axs.flat):
    ax.imshow(images[i], cmap='gray')
    ax.set_title(f'Image {i+1}')
    ax.axis('off')

# Connect the click event
main_fig.canvas.mpl_connect('button_press_event', on_click)

plt.show()

from matplotlib.

tacaswell avatar tacaswell commented on July 28, 2024 1

Similarly, you could change the position of clicked axes

import matplotlib.pyplot as plt
import numpy as np

cache = {}

def on_click(event):
    ax = event.inaxes
    if ax is None:
        return
    if ax not in cache:
        cache[ax] = ax.get_subplotspec()
        ax.set_position([0, 0, 1, 1])
        ax.set_zorder(5)
    else:
        sp = cache.pop(ax)
        ax.set_subplotspec(sp)
        ax.set_zorder(1)        
        ax.set_in_layout(True)
    print(ax.get_subplotspec())
    ax.figure.canvas.draw_idle()
    
def create_random_image():
    return np.random.rand(100, 100)

# Generate four random images
images = [create_random_image() for _ in range(4)]

# Create a main figure to display the images
main_fig, axs = plt.subplots(2, 2, layout='compressed')

for i, ax in enumerate(axs.flat):
    ax.imshow(images[i], cmap='gray')
    ax.set_title(f'Image {i+1}')
    ax.axis('off')

# Connect the click event
main_fig.canvas.mpl_connect('button_press_event', on_click)

plt.show()

from matplotlib.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.