Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 为什么可以';我不能在熊猫柱状图上得到一个标题吗?_Python 3.x_Pandas_Matplotlib_Seaborn - Fatal编程技术网

Python 3.x 为什么可以';我不能在熊猫柱状图上得到一个标题吗?

Python 3.x 为什么可以';我不能在熊猫柱状图上得到一个标题吗?,python-3.x,pandas,matplotlib,seaborn,Python 3.x,Pandas,Matplotlib,Seaborn,在您将这个问题标记为已回答之前,我已经尝试了其他几种解决方案,但它们都不起作用 我在Spyder、Python 3.7.1、pandas 0.23.4和matplotlib 3.0.2中执行以下操作: import pandas as pd import numpy as np import matplotlib import seaborn as sns (some other code to build the dataframe) shoot_merge['population'].pl

在您将这个问题标记为已回答之前,我已经尝试了其他几种解决方案,但它们都不起作用

我在Spyder、Python 3.7.1、pandas 0.23.4和matplotlib 3.0.2中执行以下操作:

import pandas as pd
import numpy as np
import matplotlib
import seaborn as sns

(some other code to build the dataframe)
shoot_merge['population'].plot.hist(bins=30, figsize=(12,8), grid=True)
好的,到目前为止还不错。它打印到屏幕上。它的y标签是“频率”,但我需要一个标题和x标签

其他解决方案看起来像这样,它们可能在Jupyter中工作,但我在Spyder中工作。我为熊猫、matplotlib和Seaborn尝试了几种解决方案,但没有一种对我有效。像这样:

gg = shoot_merge['population'].plot.hist(bins=30, figsize=(12,8), grid=True)
gg.set_xlabel("what the hell")
gg.set_title("Histogram of shootings by population")
我得到这个输出,但它不会打印到屏幕上:

gg.set_xlabel("what the hell")
Out[169]: Text(0.5, 3.200000000000003, 'what the hell')

gg.set_title("Histogram of shootings by population")
Out[170]: Text(0.5, 1.0, 'Histogram of shootings by population')

gg
Out[172]: <matplotlib.axes._subplots.AxesSubplot at 0x1da041aa8d0>
gg.set\u xlabel(“该死的”)
Out[169]:文本(0.5,3.2000000000000003,'该死的')
gg.set_title(“按人口划分的枪击事件直方图”)
Out[170]:文本(0.5,1.0,'人口射击直方图')
游戏打得好
出[172]:
我知道“gg”是matplotlib对象,那么绘图在哪里

我喜欢Python,但定义图形很痛苦。

您可以尝试以下方法:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

gg = shoot_merge['population'].plot.hist(bins=30, figsize=(12,8), grid=True)
gg.set_xlabel("what the hell")
gg.set_title("Histogram of shootings by population")
plt.show()
希望这对您有用。

您可以尝试以下方法:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

gg = shoot_merge['population'].plot.hist(bins=30, figsize=(12,8), grid=True)
gg.set_xlabel("what the hell")
gg.set_title("Histogram of shootings by population")
plt.show()

希望这对你有用。

由ImportanceofbeingErnest回答

由ImportanceofbeingErnest回答

不,我说我在Spyder工作,不是在Jupyter。但是如果我只使用Jupyter,我可能会节省时间。您是否将这些命令写入Spyder中的IPython控制台?关于matplotlib,您的Spyder设置是什么?如果您正在使用IPython的内联后端,并且希望在对其进行更改后再次显示该图形,则需要说明该图形,在您的示例中为
gg.figure
。(我建议使用
ax
而不是
gg
,以与此处的所有文档和问答保持一致)。啊,我现在看到了……gg.figure。谢谢它有标签。太好了。不,我说我在Spyder,不是Jupyter。但是如果我只使用Jupyter,我可能会节省时间。您是否将这些命令写入Spyder中的IPython控制台?关于matplotlib,您的Spyder设置是什么?如果您正在使用IPython的内联后端,并且希望在对其进行更改后再次显示该图形,则需要说明该图形,在您的示例中为
gg.figure
。(我建议使用
ax
而不是
gg
,以与此处的所有文档和问答保持一致)。啊,我现在看到了……gg.figure。谢谢它有标签。伟大的