当前文件名为: matplotlib.py
from matplotlib import pyplot
years = [1950,1960,1970,1980,1990,2000,2010]
gdb = [300.2,543.3,1075.9,2862.5,5979.6,10289.7,14958.3]
# 创建一幅线图,x轴是年份,y轴是gdb
pyplot.plot(years,gdb,color='green',marker='o',linestyle='solid')
# 添加一个标题
pyplot.title('名义GDP')
# 给y轴加标记
pyplot.ylabel('十亿美元')
# 输出线图
pyplot.show()
运行报错:
Traceback (most recent call last):
File "D:/1.PCshizhan/RGZN/第三阶段:机器学习/matplotlib.py", line 1, in
from matplotlib import pyplot
File "D:\1.PCshizhan\RGZN\第三阶段:机器学习\matplotlib.py", line 1, in
from matplotlib import pyplot
ImportError: cannot import name 'pyplot' from 'matplotlib' (D:\1.PCshizhan\RGZN\第三阶段:机器学习\matplotlib.py)
把 matplotlib.py 文件名修改为别的名字,不能与模块名
from matplotlib import pyplot
相同,
修改名字为(自己定义) aaa.py 运行代码
from matplotlib import pyplot
years = [1950,1960,1970,1980,1990,2000,2010]
gdb = [300.2,543.3,1075.9,2862.5,5979.6,10289.7,14958.3]
# 创建一幅线图,x轴是年份,y轴是gdb
pyplot.plot(years,gdb,color='green',marker='o',linestyle='solid')
# 添加一个标题
pyplot.title('名义GDP')
# 给y轴加标记
pyplot.ylabel('十亿美元')
# 输出线图
pyplot.show()