无法将标题添加到饼图(matplotlib) [英] Can't add title to pie chart (matplotlib)

查看:280
本文介绍了无法将标题添加到饼图(matplotlib)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

plt.title('Survival Rate')
plt.pie(total_survival, labels=[str(survival_percent) + "% " + "survived", str(100- survival_percent) + "% " + "died"])

我要添加标题饼图,但出现此错误:

I am trying to add a title to a pie chart but am getting this error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-250-7e0f3cd8c625> in <module>()
----> 1 plt.title('Survival Rate')
  2 plt.pie(total_survival, labels=[str(survival_percent) + "% " + "survived", str(100- survival_percent) + "% " + "died"])
  3 

TypeError: 'str' object is not callable

我在网上看到的所有资源都将其标题写为括号内的字符串,并且不将其作为参数包含在 plt.pie()中,所以我我不确定我要去哪里错了。

All resources that I have seen online write their title as a string within parentheses and don't include it as an argument in plt.pie() so I'm not sure where I am going wrong.

推荐答案

以下代码可以正常工作:

The following code works fine:

import matplotlib.pyplot as plt
survival_percent = 67
total_survival = [67,33]
plt.title('Survival Rate')
plt.pie(total_survival, labels=[str(survival_percent) + "% " + "survived", str(100- survival_percent) + "% " + "died"])

plt.show()

因此,您得到的错误是由未在此处显示的部分代码产生的。
我们现在只能猜测,但是错误'str'对象是不可调用的表明,您已经重新定义了 plt.title 转换为字符串。
例如,如果在代码中的某个地方您编写了类似的内容,则可能会发生这种情况

The error you get is therefore produced by part of the code that you don't show here. We can now only guess, but the error 'str' object is not callable suggests, that you have redefined plt.title to a string. This could happen if for example somewhere in your code you wrote something like

plt.title = 'Survival Rate'

(这当然是不合理的),然后稍后调用(正确)

(which is of course not reasonable) then later on calling (correctly)

plt.title('生存率')会导致您得到错误。

这篇关于无法将标题添加到饼图(matplotlib)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆