I want to change the color by clicking the scatter point in matplotlib

at first

It was good up to the point where I plotted it with scatter, but it was difficult to update the data after that, so I will share it.

code

scatter.py


import matplotlib.pyplot as plt
import itertools #Cartesian product
import numpy as np

if __name__ == '__main__':
    x_max = 10
    y_max = 10

    fig = plt.figure(figsize=(5,5))
    ax = fig.add_subplot(111)

    ax.set_xlim(-x_max*0.05,(x_max-1)*1.05)
    ax.set_ylim(-y_max*0.05,(y_max-1)*1.05)

    x_data = [i for i in range(x_max)]
    y_data = [i for i in range(y_max)]
    tmp_data = list(itertools.product(x_data,y_data))
    t_x = [i[0] for i in tmp_data]
    t_y = [i[1] for i in tmp_data]
    data = [t_x,t_y]
    
    colors = [(0,0,0,1) for i in range(x_max*y_max)]
    art = ax.scatter(data[0],data[1],c=colors)
    
    def onclick(event):
        x = round(event.xdata,0)
        y = round(event.ydata,0)
        tmp = int(x*(y_max) + y)
        if colors[tmp] == (0,0,0,1):
            colors[tmp] = (0.5,0.5,0.5,1)
        elif colors[tmp] == (0.5,0.5,0.5,1):
            colors[tmp] = (0,0,0,1)
        art.set_facecolor(colors)
        plt.draw()


    plt.connect('button_press_event', onclick)

    plt.show()

Recommended Posts

I want to change the color by clicking the scatter point in matplotlib
I want to embed Matplotlib in PySimpleGUI
I want to display the progress in Python!
I tried to predict the change in snowfall for 2 years by machine learning
I want to write in Python! (3) Utilize the mock
I want to use the R dataset in python
I want to save the photos sent by LINE to S3
I want to change the Japanese flag to the Palau flag with Numpy
I want to make the Dictionary type in the List unique
I want to align the significant figures in the Numpy array
Notes on coloring by value in the matplotlib scatter plot
I didn't want to write the AWS key in the program
[Python] I want to make a 3D scatter plot of the epicenter with Cartopy + Matplotlib!
[Linux] I want to know the date when the user logged in
How to change the color of just the button pressed in Tkinter
LINEbot development, I want to check the operation in the local environment
I want to make the second line the column name in pandas
I want to pass the G test in one month Day 1
I want to know the population of each country in the world.
I want to create a graph with wavy lines omitted in the middle with matplotlib (I want to manipulate the impression)
I want to pin Spyder to the taskbar
I want to output to the console coolly
Specify the color in the matplotlib 2D map
I want to print in a comprehension
I want to handle the rhyme part1
I want to handle the rhyme part3
I want to display the progress bar
How to change editor color in PyCharm
I want to handle the rhyme part2
I want to handle the rhyme part5
I want to handle the rhyme part4
(Matplotlib) I want to draw a graph with a size specified in pixels
[C language] I want to generate random numbers in the specified range
I want to batch convert the result of "string" .split () in Python
I want to explain the abstract class (ABCmeta) of Python in detail.
I want to sort a list in the order of other lists
I want to use the Django Debug Toolbar in my Ajax application
I want to color a part of an Excel string in Python
[Google Colab] I want to display multiple images side by side in tiles
I want to leave an arbitrary command in the command history of Shell
I want to be healed by Mia Nanasawa's image. In such a case, hit the Twitter API ♪
I want to make matplotlib a dark theme
I want to visualize where and how many people are in the factory
I want to do Dunnett's test in Python
[Python] I want to know the variables in the function when an error occurs!
I want to pin Datetime.now in Django tests
I want to use Python in the environment of pyenv + pipenv on Windows 10
I want to create a window in Python
[Django memo] I want to set the login user information in the form in advance
I want to display multiple images with matplotlib.
I want to handle the rhyme part7 (BOW)
I want to store DB information in list
Change the decimal point of logging from, to.
I want to merge nested dicts in Python
I want to set a life cycle in the task definition of ECS
I want to manage systemd by time zone! !!
I want to see a list of WebDAV files in the Requests module
Paste a link to the data point of the graph created by jupyterlab & matplotlib
I want to store the result of% time, %% time, etc. in an object (variable)
I want to sell Mercari by scraping python
I want to get the file name, line number, and function name in Python 3.4