This is the output of the below script. This plot is supposed to be an interactive one. But since I screenshotted it, it becomes a static image. This was suppose to work like this: when a certain point is clicked (let's say we click the one pointed by the red arrow), the plot title will show the name of the country represented by the point.
Last active
December 1, 2017 15:19
-
-
Save erikaris/25684fc2bd52f9481b5a811b4e44771f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Modified from Coursera | |
import pandas as pd | |
import numpy as np | |
import matplotlib.pyplot as plt | |
from random import shuffle | |
origins = ['China', 'Brazil', 'India', 'USA', 'Canada', 'UK', 'Germany', 'Iraq', 'Chile', 'Mexico'] | |
shuffle(origins) | |
df = pd.DataFrame({'height': np.random.rand(10), | |
'weight': np.random.rand(10), | |
'origin': origins}) | |
txt = None | |
plt.figure() | |
# picker=5 means the mouse doesn't have to click directly on an event, but can be up to 5 pixels away | |
plt.scatter(df['height'], df['weight'], picker=5) | |
plt.gca().set_ylabel('Weight') | |
plt.gca().set_xlabel('Height') | |
plt.show() | |
def onpick(event): | |
global txt | |
if txt: | |
txt.remove() | |
plt.draw() | |
txt = plt.gca().text(0.03, 0.9, "event adalah {}".format(event.ind[0]), transform=plt.gca().transAxes, va='top') | |
origin = df.iloc[event.ind[0]]['origin'] | |
plt.gca().set_title('Selected item came from {}'.format(origin)) | |
# tell mpl_connect we want to pass a 'pick_event' into onpick when the event is detected | |
plt.gcf().canvas.mpl_connect('pick_event', onpick) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment