原标题:matplotlib手册(1)-pyplot使用
欢迎关注天善智能 hellobi.com,我们是专注于商业智能BI,大数据,数据分析领域的垂直社区,学习、问答、求职,一站式搞定!
对商业智能BI、大数据分析挖掘、机器学习,python,R等数据领域感兴趣的同学加微信:tstoutiao,邀请你进入数据爱好者交流群,数据爱好者们都在这儿。
最近开始看看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 deion
'-' 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_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 trianglesplt.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^')
天善学院svip包含Excel BI、Python爬虫案例、Python机器学习、Python数据科学家、大数据、数据分析报告、数据分析师体系、深度学习、R语言案例10套课程火爆报名中,欢迎大家关注 www.hellobi.com/svip
转载请保留以下内容:
本文来源自天善社区liutangwow老师的博客(公众号)。
原文链接:https://ask.hellobi.com/blog/yuguiyang1990/9215返回搜狐,查看更多
责任编辑: