David Zwicker
David Zwicker

Reputation: 24268

How can I disable the label when plotting pandas data?

I tried using label=None in the plot command, in which case matplotlib chose the key of the data as a label. I find this behavior unintuitive and expected to have full control over the label when I set it explicitly. How can I disable the label of a plot involving data from a pandas dataframe?

Here is a small example, which shows the behavior:

import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({'x': [0, 1], 'y': [0, 1]})
plt.plot(df['x'], df['y'], label=None)
plt.legend(loc='best')

This results in the following plot:

enter image description here

In case it matters, I'm currently using matplotlib version 1.5.1 and pandas version 0.18.0 in the following python installed from macports: Python 2.7.11 (default, Mar 1 2016, 18:40:10) [GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin

Upvotes: 6

Views: 9259

Answers (2)

EdChum
EdChum

Reputation: 393933

If you pass an empty string then it results in this plot:

import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({'x': [0, 1], 'y': [0, 1]})
plt.plot(df['x'], df['y'], label='')
plt.legend(loc='best')

enter image description here

Upvotes: 7

Mike
Mike

Reputation: 20196

Just pass an empty label. Matplotlib doesn't bother making a legend entry in that case. The label=None argument is the default, which the plotting function detects, telling it to make up a label.

(Of course, in this case, you could just not use plt.legend...)

Upvotes: 1

Related Questions