7

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

2
  • 1
    You can pass an empty string: plt.plot(df['x'], df['y'], label='') Commented Mar 22, 2016 at 18:34
  • Can you post this as an answer so I can accept it? Commented Mar 22, 2016 at 18:37

2 Answers 2

8

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

Sign up to request clarification or add additional context in comments.

Comments

1

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...)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.