Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Issue with interative plot and notebook style #284

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

Closed
glemaitre opened this issue Aug 7, 2017 · 6 comments
Closed

Issue with interative plot and notebook style #284

glemaitre opened this issue Aug 7, 2017 · 6 comments

Comments

@glemaitre
Copy link
Contributor

I am having an issue when trying to plot and update it using the notebook style.
I added a small examples in the details section.

I wanted to create a subplots figure and then create multiple cells (text/code) in which I would update the initial figure. However, it seems that the figure never get updated once it pass the first cell.

Is there any work-around to allow such behaviour?

Minimum examples

"""
==========================================================================
Illustration of the sample selection for the different NearMiss algorithms
==========================================================================

This example illustrates the different way of selecting example in NearMiss.

"""

import matplotlib.pyplot as plt
import numpy as np

from sklearn.neighbors import NearestNeighbors

print(__doc__)

rng = np.random.RandomState(18)

###############################################################################
# This function allows to make nice plotting


def make_plot_despine(ax):
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.get_xaxis().tick_bottom()
    ax.get_yaxis().tick_left()
    ax.spines['left'].set_position(('outward', 10))
    ax.spines['bottom'].set_position(('outward', 10))
    ax.set_xlim([0.5, 3.])
    ax.set_ylim([0.5, 3.])
    ax.set_xlabel(r'$X_1$')
    ax.set_ylabel(r'$X_2$')
    ax.legend()


###############################################################################
# We can start by generating some data to later illustrate the principle of
# each NearMiss heuritic rules.

# minority class
X_minority = np.transpose([[1.1, 1.3, 1.15, 0.8, 0.8, 0.6, 0.55],
                           [1., 1.5, 1.7, 2.5, 2.0, 1.2, 0.55]])
# majority class
X_majority = np.transpose([[2.1, 2.12, 2.13, 2.14, 2.2, 2.3, 2.5, 2.45],
                           [1.5, 2.1, 2.7, 0.9, 1.0, 1.4, 2.4, 2.9]])

plt.ioff()
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(20, 6))

# plot the majority and minority samples
for ax in (ax1, ax2, ax3):
    ax.scatter(X_minority[:, 0], X_minority[:, 1],
               label='Minority class', s=200, marker='_')
    ax.scatter(X_majority[:, 0], X_majority[:, 1],
               label='Majority class', s=200, marker='+')

###############################################################################
# NearMiss-1
###############################################################################

###############################################################################
# NearMiss-1 selects samples from the majority class for which the average
# distance to some nearest neighbours is the smallest. In the following
# example, we use a 3-NN to compute the average distance on 2 specific samples
# of the majority class. Therefore, in this case the point linked by the
# green-dashed line will be selected since the average distance is smaller.

nearest_neighbors = NearestNeighbors(n_neighbors=3)
nearest_neighbors.fit(X_minority)
dist, ind = nearest_neighbors.kneighbors(X_majority[:2, :])
# Compute the average distance
dist_avg = dist.sum(axis=1) / 3

for positive_idx, (neighbors, distance, color) in enumerate(
        zip(ind, dist_avg, ['g', 'r'])):
    for make_plot, sample_idx in enumerate(neighbors):
        ax1.plot([X_majority[positive_idx, 0], X_minority[sample_idx, 0]],
                 [X_majority[positive_idx, 1], X_minority[sample_idx, 1]],
                 '--' + color, alpha=0.3,
                 label='Avg. dist.={0:.2f}'.format(distance)
                 if make_plot == 0 else "")
ax1.set_title('NearMiss-1')
make_plot_despine(ax1)
plt.show()
@larsoner
Copy link
Contributor

larsoner commented Aug 7, 2017

This looks like a dup of #161, if so can you close and comment over there?

@choldgraf
Copy link
Contributor

choldgraf commented Aug 7, 2017

Correct me if I'm wrong, but I think what they'd like to do is have the figure display each time that it is updated. I don't think that sphinx-gallery supports this, right?

@larsoner
Copy link
Contributor

larsoner commented Aug 7, 2017

I think it's the same as in #161, namely not closing a figure so it can update + display in subsequent cells

@choldgraf
Copy link
Contributor

either way, the end-result was that this isn't supported, and that people should re-create the figure each time they want it displayed, right?

@larsoner
Copy link
Contributor

larsoner commented Aug 7, 2017

Yep. Although the more people request such a feature, the more I would be tempted to add it with the caveat that it works differently in IPython -- but that's really up to the maintainers to weigh

@glemaitre
Copy link
Contributor Author

Thanks @Eric89GXL @choldgraf
Re-create the figure will the work around if we don't find another layout for the example.

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

3 participants