折线图

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

import matplotlib.pyplot as pyplot

# init
pyplot.figure() # arguments
x_label = 'X-label'
y_label = 'Y-label'
title = 'Demo-title' # points data
x = [1, 2, 3, 4]
y = [45, 32, 46, 89] # set arguments
pyplot.xlabel(x_label)
pyplot.ylabel(y_label)
pyplot.title(title) # set data
pyplot.plot(x, y) pyplot.show()

多个函数图像

import matplotlib.pyplot as pyplot
import numpy # init
pyplot.figure() # arguments
x_label = 'x'
y_label = 'sin(x)'
title = 'Figure-sin(x)' # points data
# [0, 10] 区间内的 1000 个均匀分布的 x
x = numpy.linspace(0, 10, 1000)
sin_y = numpy.sin(x)
cos_y = numpy.cos(x) # set arguments
pyplot.xlabel(x_label)
pyplot.ylabel(y_label)
pyplot.title(title) # 设置 y 轴范围
pyplot.ylim(-1.5, 1.5) # set data
# label, color, linewidth 是 图示 参数,用于区分多个曲线的情况
pyplot.plot(x, sin_y, label='$ sin(x) $', color='red', linewidth=1)
pyplot.plot(x, cos_y, label='$ cos(x) $', color='blue', linewidth=1)
pyplot.legend() pyplot.show()

多个函数图像 2.0

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

import matplotlib.pyplot as pyplot
import numpy
import math class draw_arguments: def __init__(self, func, func_name, x_domain: tuple, points_num=1000):
super().__init__()
self.draw_func = func
self.func_name = func_name
self.x_data = numpy.linspace(x_domain[0], x_domain[1], points_num)
self.y_data = [func(x) for x in self.x_data] def draw_figure(dargs, title='Figure', x_label='x', y_label='y'):
# init
pyplot.figure() # set arguments
pyplot.xlabel(x_label)
pyplot.ylabel(y_label)
pyplot.title(title) # set data
# label, color, linewidth 是 图示 参数,用于区分多个曲线的情况
for draw in dargs:
pyplot.plot(draw.x_data, draw.y_data, label='$' +
draw.func_name + '$', linewidth=1)
pyplot.legend() pyplot.show() d1 = draw_arguments(func=lambda x: 2**x,
func_name='2^x',
x_domain=(0, 5)) d2 = draw_arguments(func=lambda x: x*x,
func_name='x^2',
x_domain=(0, 5)) 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 做了一段动画:

import matplotlib.pyplot as pyplot
import numpy
import math pyplot.rcParams['figure.figsize'] = (3, 3) # 图像显示大小
pyplot.rcParams['lines.linewidth'] = 1.5 # 设置曲线线条宽度
pyplot.ion()
data = numpy.linspace(-math.sqrt(math.pi), math.sqrt(math.pi), 500)
x, y = [], [] def heart(x):
return math.pow(x * x, 1 / 3) + math.sqrt(math.pi - x * x) * math.sin(10 * x * math.pi) for k in data:
x.append(k)
y.append(heart(k))
pyplot.clf()
subplot = pyplot.subplot()
pyplot.plot(x, y)
pyplot.pause(0.0000001) pyplot.ioff()
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. C++走向远洋——33(静态成员的应用)

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:time.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  2. git指令-撤销修改

    git指令-撤销修改 如果在文件中添加了错误的内容,可以撤销修改 eg: 解决: 可以删掉最后一行,手动把文件恢复到上一个版本的状态: 使用git status 你可以发现,Git会告诉你,git c ...

  3. 【攻略】如何在云开发中使用 Redis?

    默认情况下,云开发的函数部署在公共网络中,只可以访问公网.如果开发者需要访问腾讯云的 Redis.TencentDB.CVM.Kafka 等资源,需要建立私有网络来确保数据安全及连接安全. 本文会演示 ...

  4. Object-Oriented Programming Summary Ⅳ

    目录 UML单元总结博客 总结本单元两次作业的设计 总结自己在四个单元中架构设计以及OO方法理解的演进 总结自己在四个单元中测试理解与实践的演进 总结自己的课程收获 立足于自己的体会给课程组提三个具体 ...

  5. RNN学习笔记(一):长短时记忆网络(LSTM)

    一.前言 在图像处理领域,卷积神经网络(Convolution Nerual Network,CNN)凭借其强大的性能取得了广泛的应用.作为一种前馈网络,CNN中各输入之间是相互独立的,每层神经元的信 ...

  6. 关于integer overflow错误

    前端突然报了integer overflow错误,int类型溢出也就是数字超过了int类型,一看很懵逼,查看后台日期发现是在Math.toIntExact()方法报错 那么我们看下方法内部代码: /* ...

  7. Feign 在 SpringCloud 中的使用 四

    此处就单纯写一个消费者服务,通过Feign来调用生产者中的接口,生产者中的接口可以自己随便定义一个,前面博客中也有代码 1.导入springcloud Feign的jar包 <parent> ...

  8. PyCharm设置远程虚拟环境

    1. 创建项目 2. 配置解释器 1. 先打开srttings 2. 选择配置解释器选项 3. 配置远端虚拟环境 4. 成功提示 5. 查看pip列表信息 经过以上操作,PyCharm设置远程虚拟环境 ...

  9. LeetCode 136、137、260(只出现一次的数,异或性质及应用)

    First. 陈列一下“异或”的一些性质 异或是一种基于二进制的位运算,用符号XOR或者 ^ 表示,其运算法则是对运算符两侧数的每一个二进制位,同值取0,异值取1. 它与布尔运算的区别在于,当运算符两 ...

  10. Django中update和save()同时作用

    数据更新操作,对单条记录,可以使用save或者是update两种方式. save() 默认保存后会看到sql语句中更新了所有字段,而save的值是之前获取时候的字段值,是缓存下来的,并不一定最新,可能 ...