In [16]: %matplotlib Using matplotlib backend: Qt5Agg In [17]: import matplotlib.pyplot as plt In [18]: import numpy as np In [19]: fig = plt.figure() In [20]: plt.plot(x,np.sin(x)) Out[20]: [<matplotlib.lines.Line2D at 0x263a7348c08>] In [21]: plt.plot(x,np.cos(x)) Out[21]: [<matplotlib.lines.Line2D at 0x263a4ad45c8>] In [22]: fig.savefig('my_figure.png') #保存文件 In [26]: from IPython.display import Image In [27]: Image('my_figure.png') #查看文件 Out[27]: <IPython.core.display.Image object>
plt.plot(*args, scalex=True, scaley=True, data=None, **kwargs) #Docstring: 描述 #Plot y versus x as lines and/or markers. 将y与x绘制为线或标记 #Call signatures:: 调用方式 #plot([x], y, [fmt], *, data=None, **kwargs) #plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs) #The optional parameter *fmt* is a convenient way for defining basic #formatting like color, marker and linestyle. #可选参数*fmt*是定义基本格式,如颜色、标记和线型。 #When conflicting with *fmt*, keyword arguments take precedence. #当与*fmt*冲突时,关键字参数优先。
参数解释:
x,y:数据点的水平/垂直坐标,x值是可选的,默认为 range(len(y)) 参见如下代码:
1 2 3 4 5 6 7
%matplotlib inline import matplotlib.pyplot as plt import pandas as pd fig = plt.figure() ax = plt.axes() y = pd.Series([1,2,3,4]) ax.plot(y,'o')
而 x,y 也可以是标量或二维的。
1 2 3 4 5 6 7 8
%matplotlib inline import matplotlib.pyplot as plt import pandas as pd fig = plt.figure() ax = plt.axes() x = ((2,2),(3,3),(4,4),(5,5)) y = ((1,2),(2,3),(4,5),(5,6)) ax.plot(x,y,'o')