折线图

下面是绘制折线图,设置图片的横轴纵轴标签,图片标题的API的用法。

  1. import matplotlib.pyplot as pyplot
  2. # init
  3. pyplot.figure()
  4. # arguments
  5. x_label = 'X-label'
  6. y_label = 'Y-label'
  7. title = 'Demo-title'
  8. # points data
  9. x = [1, 2, 3, 4]
  10. y = [45, 32, 46, 89]
  11. # set arguments
  12. pyplot.xlabel(x_label)
  13. pyplot.ylabel(y_label)
  14. pyplot.title(title)
  15. # set data
  16. pyplot.plot(x, y)
  17. pyplot.show()

多个函数图像

  1. import matplotlib.pyplot as pyplot
  2. import numpy
  3. # init
  4. pyplot.figure()
  5. # arguments
  6. x_label = 'x'
  7. y_label = 'sin(x)'
  8. title = 'Figure-sin(x)'
  9. # points data
  10. # [0, 10] 区间内的 1000 个均匀分布的 x
  11. x = numpy.linspace(0, 10, 1000)
  12. sin_y = numpy.sin(x)
  13. cos_y = numpy.cos(x)
  14. # set arguments
  15. pyplot.xlabel(x_label)
  16. pyplot.ylabel(y_label)
  17. pyplot.title(title)
  18. # 设置 y 轴范围
  19. pyplot.ylim(-1.5, 1.5)
  20. # set data
  21. # label, color, linewidth 是 图示 参数,用于区分多个曲线的情况
  22. pyplot.plot(x, sin_y, label='$ sin(x) $', color='red', linewidth=1)
  23. pyplot.plot(x, cos_y, label='$ cos(x) $', color='blue', linewidth=1)
  24. pyplot.legend()
  25. pyplot.show()

多个函数图像 2.0

在上述基础上进一步封装, 对 draw_arguments 进行实例化, 然后调用 draw_figure 即可.

  1. import matplotlib.pyplot as pyplot
  2. import numpy
  3. import math
  4. class draw_arguments:
  5. def __init__(self, func, func_name, x_domain: tuple, points_num=1000):
  6. super().__init__()
  7. self.draw_func = func
  8. self.func_name = func_name
  9. self.x_data = numpy.linspace(x_domain[0], x_domain[1], points_num)
  10. self.y_data = [func(x) for x in self.x_data]
  11. def draw_figure(dargs, title='Figure', x_label='x', y_label='y'):
  12. # init
  13. pyplot.figure()
  14. # set arguments
  15. pyplot.xlabel(x_label)
  16. pyplot.ylabel(y_label)
  17. pyplot.title(title)
  18. # set data
  19. # label, color, linewidth 是 图示 参数,用于区分多个曲线的情况
  20. for draw in dargs:
  21. pyplot.plot(draw.x_data, draw.y_data, label='$' +
  22. draw.func_name + '$', linewidth=1)
  23. pyplot.legend()
  24. pyplot.show()
  25. d1 = draw_arguments(func=lambda x: 2**x,
  26. func_name='2^x',
  27. x_domain=(0, 5))
  28. d2 = draw_arguments(func=lambda x: x*x,
  29. func_name='x^2',
  30. x_domain=(0, 5))
  31. draw_figure([d1, d2])

绘制动画

心形曲线

提到心形曲线, 最著名的莫过于笛卡尔心形曲线, 其方程为(极坐标的形式):

\[r = a(1 - \sin{\theta})
\]

但我不是很想用这个.

还有一个较为著名的方程形式的心形曲线:

\[x^2+(y-x^\frac{2}{3})^2 = 0
\]

也不是很想用, 因为这都不是函数形式.

偶然发现了一个心形曲线为:

\[f(x) = x^\frac{2}{3} + \sqrt{\pi-x^2} \sin(k \pi x)
\]

其中, \(k \ge 10\) 时, 随着 \(k\) 的增大, 函数图像会越来趋近于一个心形.

当 \(k=10\) 时:

闲着没事, 用 python 做了一段动画:

  1. import matplotlib.pyplot as pyplot
  2. import numpy
  3. import math
  4. pyplot.rcParams['figure.figsize'] = (3, 3) # 图像显示大小
  5. pyplot.rcParams['lines.linewidth'] = 1.5 # 设置曲线线条宽度
  6. pyplot.ion()
  7. data = numpy.linspace(-math.sqrt(math.pi), math.sqrt(math.pi), 500)
  8. x, y = [], []
  9. def heart(x):
  10. return math.pow(x * x, 1 / 3) + math.sqrt(math.pi - x * x) * math.sin(10 * x * math.pi)
  11. for k in data:
  12. x.append(k)
  13. y.append(heart(k))
  14. pyplot.clf()
  15. subplot = pyplot.subplot()
  16. pyplot.plot(x, y)
  17. pyplot.pause(0.0000001)
  18. pyplot.ioff()
  19. pyplot.show()

pyplot 作图总结的更多相关文章

  1. Python#常用的模块和简单用法

    目录 random 随机模块 os 文件夹模块: time 时间模块: matplotlab.pyplot 作图模块 mpl_toolkits.mplot3d 绘制3D图模块 Pygame Reque ...

  2. Python与R的区别和联系

    转自:http://bbs.pinggu.org/thread-3078817-1-1.html 有人说Python和R的区别是显而易见的,因为R是针对统计的,python是给程序员设计的,其实这话对 ...

  3. 6 python高级数据处理和可视化

    6.2. pyplot作图 1.折线图和散点图 t = np.arange(0,4,0.1) plt.plot(t,t,'o',t,t+2,t,t**2,'o') plt.show() 2.柱线图 p ...

  4. Matplotlib数据可视化(1):入门介绍

      1 matplot入门指南¶ matplotlib是Python科学计算中使用最多的一个可视化库,功能丰富,提供了非常多的可视化方案,基本能够满足各种场景下的数据可视化需求.但功能丰富从另一方面来 ...

  5. matplotlib画图实例:pyplot、pylab模块及作图參数

    http://blog.csdn.net/pipisorry/article/details/40005163 Matplotlib.pyplot画图实例 {使用pyplot模块} matplotli ...

  6. Python: 作图

    在python中实现数据的可视化,也即作图,一般是依赖matplotlib宏包实现的.但常见的代码中都是加载pylab,是不是这里写错了呀?其实pylib只是matplotlib的一个模块,只是被做成 ...

  7. matplotlib 入门之Pyplot tutorial

    文章目录 pyplot 介绍 修饰你的图案 格式字符串 [color][marker][line] Colors Markers Line Styles 利用关键字作图(大概是数据映射到属性吧) 传入 ...

  8. 用matplotlib获取雅虎股票数据并作图

    matplotlib有一个finance子模块提供了一个获取雅虎股票数据的api接口:quotes_historical_yahoo_ochl 感觉非常好用! 示例一 获取数据并作折线图 import ...

  9. 一个简单的使用matplotlib作图的例子

    #使用matplotlib作图 import numpy as np import matplotlib.pyplot as plt #x = np.linspace(start, stop, num ...

随机推荐

  1. AndroidStudio自动导入包

    IntelliJ IDEA可以自动优化导入包,但是有多个同名的类位于不同的包时,需要自己手动使用Alt + Enter进行导入. Settings→Editor→General→Auto Import ...

  2. Microsoft Translator:消除面对面交流的语言障碍

    ​ Translator:消除面对面交流的语言障碍" title="Microsoft Translator:消除面对面交流的语言障碍"> ​ James Simm ...

  3. 人心和隐私怎么防?“防出轨”APP让道德滑落

    ​ 王尔德曾说过,"一个人应该永远保持一点神秘感".让·保·里克特也表示,:"一个人泄露了秘密,哪怕一丝一毫,就再也得不到安宁了".可见,对于自然人来说,保有自 ...

  4. 11--PHP中的类和对象

    PHP类和对象 类是面向对象程序设计的基本概念,通俗的理解类就是对现实中某一个种类的东西的抽象, 比如汽车可以抽象为一个类,汽车拥有名字.轮胎.速度.重量等属性,可以有换挡.前进.后退等操作方法. 通 ...

  5. Java基础IO流 ,文件读取,由易至难

    最基础的读取文件 import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;imp ...

  6. 使用 Hexo 创建项目文档网站

    当我们发布一个开源项目的时候,最重要的事情之一就是要创建项目文档.对使用项目的用户来说,文档是非常有必要的,通常我们可以使用下面这些方式来创建文档: GitHub Wiki:在 Github 上我们可 ...

  7. Canvas 使用及应用

    Canvas canvas 是 HTML5 当中我最喜欢的所有新特性中我最喜欢的一个标签了.因为它太强大了,各种有意思的特效都可以实现. 1. canvas 的基本使用方法 - 它是一个行内块元素 - ...

  8. Vue项目二、vue环境搭建以及Vue-cli使用及详解

    一.Vue多页面应用的环境搭建 每一次页面跳转的时候,后台服务器都会给返回一个新的html文档,这种类型的网站也就是多页网站,也叫做多页应用. 环境的搭建如下,在页面中引入如下框架 <scrip ...

  9. Python魔法方法之 __call__

    前言 Python的魔法方法是指Python内部已经包含的,被双下划线所包围的方法,这些方法在特定的操作时会自动被调用.魔法方法可以使Python的自由度变得更高,当不重载魔法方法时它可以在规定的默认 ...

  10. python装饰器见解笔记

    def zsq(fun): def zsq_n(*args,**kwargs) print('这是装饰器需要运行内容') r = fun(*args,**kwargs) print('在被装饰函数执行 ...