41

Is there a way to add title (and xlabel and ylabel) to plt.scatter(x,y,...) or plt.plot(x,y,...) directly without writing additional lines?

It is easy to add it when we use Series_name.plot in which we simply write Series_name.plot(...,title='name') but it does not work for me if I write: plt.scatter(...,title='name') or plt.plot(...,title='name')

[plt<< import matplotlib.pyplot as plt]

I am using Python 3.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
shm2008
  • 1,393
  • 1
  • 11
  • 10
  • Please tell us what you have tried already and possibly add a code snippet of what does not work as you expect. – Bas Jansen Feb 14 '17 at 10:25
  • 2
    Not necessarily an exact duplicate, but the things you need can be find easily by just googling: http://stackoverflow.com/questions/12444716/how-do-i-set-the-figure-title-and-axes-labels-font-size-in-matplotlib – pingul Feb 14 '17 at 10:27
  • 2
    Possible duplicate of [How do I set the figure title and axes labels font size in Matplotlib?](http://stackoverflow.com/questions/12444716/how-do-i-set-the-figure-title-and-axes-labels-font-size-in-matplotlib) – pingul Feb 14 '17 at 10:27
  • 1
    Thanks guys! I'm curious to know if we can write all of that in the parenthesis of plt.scatter()? – shm2008 Feb 14 '17 at 10:35
  • No, it's not possible to add the arguments to the `plt.scatter()` call. – pingul Feb 14 '17 at 10:53
  • But neither is it to use those arguments in `plot()`, can anyone report that `plt.plot(...,title='name')` is actually working? – ImportanceOfBeingErnest Feb 14 '17 at 10:56
  • plt.plot(...,title='name') does not work but Series_name.plot(...,title='name') works. – shm2008 Feb 14 '17 at 11:43
  • @shm Those two commands have nothing in common. One is a `matplotlib.pyplot` command the other is a `pandas.Series` command. In order to use pandas, you can of course plot a scatter plot via `Series_name.plot(kind="scatter", ..., ,title='name')`, see [the documentation](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.plot.html). – ImportanceOfBeingErnest Feb 14 '17 at 11:50
  • @ImportanceOfBeingErnest Exactly the answer I was looking for. Thanks a lot! – shm2008 Feb 14 '17 at 12:11

3 Answers3

72

From the documentation of plt.scatter() there is no such arguments to set the title or labels.

But neither does the plt.plot() command have such arguments. plt.plot(x,y, title="title") throws an error AttributeError: Unknown property title. So I wonder why this should work in either case.

In any case, the usual way to set the title is plt.title. The usual way to set the labels is plt.xlabeland plt.ylabel.

import matplotlib.pyplot as plt

x= [8,3,5]; y = [3,4,5]
plt.scatter(x,y)
plt.title("title")
plt.xlabel("x-label")
plt.ylabel("y-label")
plt.show()
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
11

It's as simple as adding:

plt.xlabel('your xlabel')
plt.ylabel('your ylabel')
plt.title('your title')

after the plt.scatter() command. Then, write plt.show() to display the image with the labels and titles. You may read about it more here: http://matplotlib.org/users/text_intro.html

Moran Neuhof
  • 426
  • 4
  • 6
7

You cannot add title, xlabel, ylabel etc w/o additional lines but you can use a single additional line if you think this is better

import matplotlib.pyplot as plt
plt.scatter([1,2,3,4,5,6],[3,5,3,2,4,7])
plt.gca().update(dict(title='SCATTER', xlabel='x', ylabel='y', ylim=(0,10)))

This approach has more sense if you want to apply the same attributes to a bunch of plots, because you can prepare in advance a dictionary to set all the parameters that you want to apply to each plot.

enter image description here

gboffi
  • 22,939
  • 8
  • 54
  • 85