美文网首页我爱编程
Matplotlib-PyplotTutorial(上)

Matplotlib-PyplotTutorial(上)

作者: Vagitus | 来源:发表于2018-02-10 01:46 被阅读0次

Import

import matplotlib.pyplot as plt

plot()原型

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

1.单数组

  • e.g:
plt.plot([1,3,5])
plt.show()

如果参数只提供一个一维数组
这个一维数组就是y值的集合
Matplotlib会自动默认为所对应的x值的集合为从0开始,依次递增的同样大小的数组
提供的参数为[1,3,5] 默认的为 [0,1,2]
所以形成 (0, 1), (1, 3), (2, 5) 三个点
所以 线条 经过这三个点
result:

2.两个参数,两个单数组

  • e.g:
plt.plot([1,2,5], [2,4,6])
plt.xlabel("x values")
plt.ylabel("y values")
plt.show()

组成(1,2), (2,4), (5,6) 三个点
依次用直线连接

result:
ext:

xlabel('something') x坐标轴上显示的东西
ylabel('something') y坐标轴上显示的东西

3.设置plot的样式,第三个参数

plt.plot([1, 3, 4], [1, 3, 9], 'ro')
plt.axis([0, 5, 0, 10])
plt.show()
result:

第三个参数就是点和线的样式
可选的有:
点和线 样式:

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

点和线 颜色:

character color
‘b’ blue
‘g’ green
‘r’ red
‘c’ cyan
‘m’ magenta
‘y’ yellow
‘k’ black
‘w’ white

你也可以使用别的方式来指定颜色:
使用可选参数 ccolor:
颜色可以使用green, #00FF00, RGB元组:(0,1,0,1), 灰度0.5

设置坐标大小函数:axis()

参数为[Xmin, Xmax, Ymin, Ymax]

4.表示函数方程

  • e.g: 表示y = x^2 + 2x + 1
import numpy as np
t = np.arange(0., 5., 0.1)
plt.plot(t, t**2 + 2*t + 1, "r-")
plt.show()
result:

使用numpy, 创建一系列的x值
从0开始到5结束,间隔0.1
plot的两个参数的数组每个数值分别对应的就是
y = x^2 + 2x + 1 的 x和y值

5.多种图形

names = ['group_a', 'group_b', 'group_c']
values = [1, 10, 100]
plt.figure(1, figsize=(9, 3))

plt.subplot(131)
plt.bar(names, values)
plt.subplot(132)
plt.scatter(names, values)
plt.subplot(133)
plt.plot(names, values)
plt.suptitle('Categorical Plotting')
plt.show()
result:

bar()是柱状图
scatter()是散点图
plot()是折线图

ext:

subplot()用来同时显示多张图
参数为 'xyn'
x,y为图的规模, n是第几张图


欢迎关注我的博客Vagitus – Pythonista

相关文章

  • Matplotlib-PyplotTutorial(上)

    Import plot()原型matplotlib.pyplot.plot(*args, **kwargs) 1....

  • Matplotlib-PyplotTutorial(下)

    控制线条的属性 linewidth 线宽 dash style 折角样式 antialiased 反锯齿 ... ...

  • 重逢既是上上上上上上签

    秦一一&陆零零 双女主纯爱小说 第一章 熟悉的声音 “一一,快过来和我一起吹蜡烛啊” “一一?一一?” “一一,我...

  • 重逢既是上上上上上上签

    第二章 酒会 (喧闹声) 坐在椅子上的秦一一,身穿一身酒红色的紧身连衣裙,外披着白色西服,心不在焉地摇晃着酒杯 唉...

  • 我的上上上上上个手机

    十年前用的anycall滑盖,还挂了个七仔。记得那是一个非常闷热的暑假,我坐115路公交在八一桥站下车,要去到马路...

  • 上,不上

    2018年 6月28日 星期四 大雨 下半年,大宝要上小学了。 之前在画画班时,听了阿姨们的谈话,就觉得还是有必要...

  • 上·路·上

    必须是在路上 可以是用脚去拷问生命的宽度 也可以是用灵魂去追寻已知的终点 再鲜活也不要定格 一路上迎接那个比狂沙更...

  • 上邪 (上)

    【1】 斜阳西下,余晖不减。一辆马车从黄沙漫漫的古道缓缓而来。 不一会儿,便到了村口来往旅人歇脚的草亭。 跟随马车...

  • 上上之才

    天天有进步,可称上上之才。 古今人才多,上上之才少。 孔子一生好学,从无名之人到教化天下,才干可谓奇矣。 观孔子一...

  • 我(上上)

    5~12岁,过着美好的童年的生活。那是我刚上学的年纪,在学校的我,喜欢出风头。总喜欢和老师对着干,但因为我成绩异常...

网友评论

    本文标题:Matplotlib-PyplotTutorial(上)

    本文链接:https://www.haomeiwen.com/subject/wbzqtftx.html