GusG
GusG

Reputation: 363

Seaborn plot goes blank when adding legend

I just started with a tutorial on Seaborn from a book; 'Python Data Science Handbook'. When I execute the part about adding a legend to the plot, the plot figures goes blank.

I entered these lines of code one by one in my Spyder console

import matplotlib.pyplot as plt
import numpy as np

rng = np.random.RandomState(0)
x = np.linspace(0, 10, 500)
y = np.cumsum(rng.randn(500, 6), 0)
plt.plot(x, y)

Plot after executing previous line

Then followed by

plt.legend('ABCDEF', ncol=2, loc='upper left');

enter image description here

Can't understand why the plot figure goes blank. And I have the latest version of Seaborn installed.

Upvotes: 0

Views: 675

Answers (1)

H.Bukhari
H.Bukhari

Reputation: 2251

You have to run code together:

import matplotlib.pyplot as plt
import numpy as np

rng = np.random.RandomState(0)
x = np.linspace(0, 10, 500)
y = np.cumsum(rng.randn(500, 6), 0)
plt.plot(x, y)
plt.legend('ABCDEF', ncol=2, loc='upper left');

this results:

enter image description here

but fi we run first just plotting and on separate cell legend, we will get:

enter image description here

because plt.legend by itself tries to create legend on empty fig that has no data

Upvotes: 2

Related Questions