matplotlib手册(1)-pyplot使用

最近开始看看Pandas中可视化的部分,主要是使用matplotlib,这里,我们先从pyplot开始。
主要参考官方教程:Pyplot tutorial
我们从一个最简单的例子开始

import matplotlib.pyplot as plt

#
plt.plot([1,2,3,4])
#指定y轴标签
plt.ylabel('some numbers')
#展示图片
plt.show()

只要几行代码,就显示了一个图形,我们观察一下这个图,会发现,y轴是1到4,而x轴是1到3,这是由于我们传入的参数导致的
plot([1,2,3,4]),这里呢,我们只传了一个数组,matplotlib会默认当做y轴的值,x轴是默认分配的,Python中ranges默认是从0开始的,所以x轴从0-3
我们也可以指定x轴的值

import matplotlib.pyplot as plt

#默认是y轴的值
#plt.plot([1,2,3,4])
#传入x轴,y轴
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
#指定y轴标签
plt.ylabel('some numbers')
#展示图片
plt.show()

matplotlib.pyplot.plot(*args, **kwargs)

这个plot呢,还可以接收一个参数,来控制绘制图形的颜色和类型,这里默认是'','b'表示颜色是blue,'-'表示solid line
详细的见下面的介绍,从官网copy来的http://matplotlib.org/2.0.2/api/pyplot_api.html#matplotlib.pyplot.legend

The following format string characters are accepted to control the line style or marker:
character   description
'-'     solid line style
'--'    dashed line style
'-.'    dash-dot line style
':'     dotted line style
'.'     point marker
','     pixel marker
'o'     circle marker
'v'     triangle_down marker
'^'     triangle_up marker
'<'     triangle_left marker
'>'     triangle_right marker
'1'     tri_down marker
'2'     tri_up marker
'3'     tri_left marker
'4'     tri_right marker
's'     square marker
'p'     pentagon marker
'*'     star marker
'h'     hexagon1 marker
'H'     hexagon2 marker
'+'     plus marker
'x'     x marker
'D'     diamond marker
'd'     thin_diamond marker
'|'     vline marker
'_'     hline marker
The following color abbreviations are supported:
character   color
‘b’     blue
‘g’     green
‘r’     red
‘c’     cyan
‘m’     magenta
‘y’     yellow
‘k’     black
‘w’     white

我们按照上面的介绍,这里我们改一下展示效果,我们改为'ro',红色,圆圈标记

plt.plot([1, 2, 3, 4], [1, 4, 9, 16] ,'ro')

就颜色来说,上面只介绍了几种颜色的缩写,那我们想用其他颜色咋办呢?
我们可以直接用参数指定,详细参数介绍,可以看官网介绍:http://matplotlib.org/2.0.2/api/pyplot_api.html

plt.plot([1, 2, 3, 4], [1, 4, 9, 16] ,color='green',
         marker='^',markersize=15,markerfacecolor='red')

我们再观察这个图,前面我们说过,只传一个list,会初始化y轴,传入2个,会初始化x,y;这里继续观察x轴和y轴
会发现他们的起始点不一样,同样,我们是可以修改这个轴的

matplotlib.pyplot.axis(*v, **kwargs)

plt.plot([1, 2, 3, 4], [1, 4, 9, 16] ,color='green',
         marker='^',markersize=15,markerfacecolor='red')

#设置轴坐标,[xmin, xmax, ymin, ymax]
plt.axis([1,8,1,32])

plot这个函数很有意思,我们最后来看个例子

import numpy as np
import matplotlib.pyplot as plt

# evenly sampled time at 200ms intervals
t = np.arange(0., 5., 0.2)

print(t)
#他直接传入了好多组参数,绘制了3条线,这里,我们也可以拆开分别绘制
# red dashes, blue squares and green triangles
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')

plt.show()
#分别绘制
#plt.plot(t, t, 'r--')
#plt.plot(t, t**2, 'bs')
#plt.plot(t, t**3, 'g^')
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容