运行pycharm时报错:
Traceback (most recent call last):
File "D:\Python\lib\site-packages\IPython\core\interactiveshell.py", line 3444, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-d8ab1186c7cc>", line 1, in <module>
runfile('D:/Pycharm/PythonProject/test.py', wdir='D:/Pycharm/PythonProject')
File "D:\Pycharm\pycharm\PyCharm 2022.2.1\plugins\python\helpers\pydev\_pydev_bundle\pydev_umd.py", line 198, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "D:\Pycharm\pycharm\PyCharm 2022.2.1\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "D:/Pycharm/PythonProject/test.py", line 1, in <module>
from pyecharts import Bar
ImportError: cannot import name 'Bar' from 'pyecharts' (D:\Python\lib\site-packages\pyecharts\__init__.py)
typeerror: __init__() takes from 1 to 2 positional arguments but 3 were given
原代码:
from pyecharts import Bar
v1 = [70, 85, 95, 64]
str1 = ['数学', '物理', '化学', '英语']
bar1 = Bar('柱状图', '分数')
bar1.add('成绩', str1, v1, is_more_utils=True)
bar1.render()
运行结果:
原因:函数使用方法不正确
解决:
写法一:
from pyecharts.charts import Bar
from pyecharts import options as opts
v1 = [70, 85, 95, 64]
str = ['数学', '物理', '化学', '英语']
bar = (
Bar()
.add_xaxis(str)
.add_yaxis('销售量', v1)
.set_global_opts(title_opts=opts.TitleOpts(title='柱状图', subtitle='分数'))
)
bar.render("bar.html")
运行结果:
写法二:
from pyecharts.charts import Bar
from pyecharts import options as opts
bar = Bar()
bar.add_xaxis(["数学", "物理", "化学", "英语"])
bar.add_yaxis("成绩", [70, 85, 95, 64])
bar.set_global_opts(title_opts=opts.TitleOpts(title="柱状图", subtitle="分数"))
bar.render("bar.html")
运行结果: