Python Plt.Scatter:如何添加标题和 xlabel 和 ylabel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42223587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Plt.Scatter: How to add title and xlabel and ylabel
提问by shm2008
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?
有没有办法直接将标题(以及 xlabel 和 ylabel)添加到 plt.scatter(x,y,...) 或 plt.plot(x,y,...) 而无需编写额外的行?
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')
当我们使用 Series_name.plot 时很容易添加它,我们只写 Series_name.plot(...,title='name') 但如果我写: plt.scatter(..., title='name') 或 plt.plot(...,title='name')
[plt<< import matplotlib.pyplot as plt]
[plt<< 将 matplotlib.pyplot 导入为 plt]
I am using Python 3.
我正在使用 Python 3。
回答by ImportanceOfBeingErnest
From the documentation of plt.scatter()
there is no such arguments to set the title or labels.
从文档中plt.scatter()
没有这样的参数来设置标题或标签。
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.
但是plt.plot()
命令也没有这样的参数。plt.plot(x,y, title="title")
引发错误AttributeError: Unknown property title
。所以我想知道为什么这应该在任何一种情况下都有效。
In any case, the usual way to set the title is plt.title
. The usual way to set the labels is plt.xlabel
and plt.ylabel
.
无论如何,设置标题的常用方法是plt.title
. 设置标签的常用方法是plt.xlabel
和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()
回答by Moran Neuhof
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
在 plt.scatter() 命令之后。然后,写入plt.show()
以显示带有标签和标题的图像。您可以在此处阅读更多信息:http: //matplotlib.org/users/text_intro.html
回答by gboffi
You cannot add title, xlabel, ylabel etc w/o additional lines but you can use a single additional lineif you think this is better
您不能添加标题、xlabel、ylabel 等而无需附加行,但如果您认为这样更好,则可以使用一个附加行
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.
如果要将相同的属性应用于一堆图,这种方法更有意义,因为您可以提前准备一个字典来设置要应用于每个图的所有参数。