matplotlib.pyplot失败记录

 

1.拷贝例子,运行

 

import numpy as np
import matplotlib.pyplot as plt
#X轴、Y轴数据
X = np.linspace(-1.5 * np.pi, 2 * np.pi, 256, endpoint=True)
C = np.cos(X)
#创建figure对象
fig = plt.figure(figsize=(10,4), dpi=100, tight_layout=True)
#figure对象获取axes对象
ax  = fig.add_subplot(111)
#axes对象绘图
ax.plot(X,   C,   color="blue",   linewidth=2.5,   linestyle="-",
label="cosine")
#axes对象调整坐标轴
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.spines['bottom'].set_position(('data',0))
ax.spines['left'].set_position(('data',0))
#axes对象获取axis对象
xAxis = ax.get_xaxis()
yAxis = ax.get_yaxis()
#axis对象设置数据轴宽度、修改刻度、刻度标签
xAxis.set_data_interval(X.min()*1.1, X.max()*1.1)
xAxis.set_ticks([-1.5 * np.pi, -np.pi, -np.pi/2, 0, np.pi/2, np.pi,
1.5 * np.pi, 2*np.pi])
xAxis.set_ticklabels([r'$-3/2\pi$', r'$-\pi$', r'$-\pi/2$', r'$0$',
r'$+\pi/2$', r'$+\pi$', r'$+3/2\pi$', r'$+2\pi$'])
yAxis.set_data_interval(C.min()*1.1, C.max()*1.1)
yAxis.set_ticks([-1, +1])
yAxis.set_ticklabels([r'$-1$', r'$+1$'])
#axis对象设置刻度位置
xAxis.set_ticks_position('bottom')
yAxis.set_ticks_position('left')
#figure添加ax2对象,与ax共用一个区域
ax2  = fig.add_subplot(111)
#axes对象作图,以及添加注释
t = 2*np.pi/3
ax2.plot([t, t], [0, np.cos(t)], color ='red', linewidth=1.5, linestyle=
"--")
ax2.scatter([t, ], [np.cos(t), ], 50, color ='blue')
ax2.annotate(r'$\cos(\frac{2\pi}{3})=-\frac{1}{2}$',
xy=(t, np.cos(t)),  xycoords='data',
xytext=(-90, -50), textcoords='offsetpoints', fontsize=16,
arrowprops=dict(arrowstyle="->", connectionstyle="arc3, rad=.2"))
#axes对象添加图例
ax.legend(loc='upperleft', frameon=False)
#展示图像
fig.show()

2.错误提示

 

runfile('/Users/keny/PythonProjects/flaskdemo2/cos.py', wdir='/Users/keny/PythonProjects/flaskdemo2')
/Users/keny/PythonProjects/flaskdemo2/cos.py:34: MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance.  In a future version, a new instance will always be created and returned.  Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.
  ax2  = fig.add_subplot(111)
/Users/keny/PythonProjects/flaskdemo2/cos.py:45: MatplotlibDeprecationWarning: Unrecognized location 'upperleft'. Falling back on 'best'; valid locations are
    best
    upper right
    upper left
    lower left
    lower right
    right
    center left
    center right
    lower center
    upper center
    center
This will raise an exception in 3.3.
  ax.legend(loc='upperleft', frameon=False)
Traceback (most recent call last):
  File "", line 1, in
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/keny/PythonProjects/flaskdemo2/cos.py", line 47, in
    fig.show()
  File "/Users/keny/PythonProjects/flaskdemo2/venv/lib/python3.7/site-packages/matplotlib/figure.py", line 443, in show
    manager.show()
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm_matplotlib_backend/backend_interagg.py", line 99, in show
    self.canvas.show()
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm_matplotlib_backend/backend_interagg.py", line 64, in show
    self.figure.tight_layout()
  File "/Users/keny/PythonProjects/flaskdemo2/venv/lib/python3.7/site-packages/matplotlib/cbook/deprecation.py", line 358, in wrapper
    return func(*args, **kwargs)

    raise ValueError("%s is not a recognized coordinate" % s)
ValueError: offsetpoints is not a recognized coordinate

你可能感兴趣的:(python实践大全)