折线图

下面是绘制折线图,设置图片的横轴纵轴标签,图片标题的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. 初学qt——数据库连接

    连接数据库我们需要有相应的dll文件,不同的数据库用不同的文件,对应的dll这里就不提供了,网上一搜一堆,就只说下这些文件的存放位置吧. 找到对应的dll文件后打开自己安装的qt的文件目录,将dll文 ...

  2. 7-12 产生每位数字相同的n位数 (30 分)

    读入2个正整数A和B,1<=A<=9, 1<=B<=10,产生数字AA...A,一共B个A 输入格式: 在一行中输入A和B. 输出格式: 在一行中输出整数AA...A,一共B个 ...

  3. 01 Taro_Mall 开源多端小程序框架设计

    项目介绍 Taro_Mall是一款多端开源在线商城应用程序,后台是基于litemall基础上进行开发,前端采用Taro框架编写,现已全部完成小程序和h5移动端,后续会对APP,淘宝,头条,百度小程序进 ...

  4. 支持IE6、IE7、IE8等低端浏览器的简化版vue

    最近研究Vue的底层原理,写了一个简化版的Vue,可以在支持IE6.IE7.IE8等低端浏览器运行.由于低端浏览器不支持对象属性定义,所以设置属性不支持直接赋值,需要调用虚拟机实例的set方法.目前只 ...

  5. python之路-基本数据类型之list列表

    1.概述 列表是python的基本数据类型之一,是一个可变的数据类型,用[]方括号表示,每一项元素使用逗号隔开,可以装大量的数据 #先来看看list列表的源码写了什么,方法:按ctrl+鼠标左键点li ...

  6. DirectX11--深入理解Effects11、使用着色器反射机制(Shader Reflection)实现一个复杂Effects框架

    前言 如果之前你是跟随本教程系列学习的话,应该能够初步了解Effects11(现FX11)的实现机制,并且可以编写一个简易的特效管理框架,但是随着特效种类的增多,要管理的着色器.资源等也随之变多.如果 ...

  7. java CRC16 算法

    代码摘自:https://www.cnblogs.com/lujiannt/p/9246256.html 1.CRC16算法 public class CRC16Util { /** * 计算CRC1 ...

  8. 如何分析SpringBoot源码模块及结构?--SpringBoot源码(二)

    注:该源码分析对应SpringBoot版本为2.1.0.RELEASE 1 前言 本篇接 如何搭建自己的SpringBoot源码调试环境?--SpringBoot源码(一). 前面搭建好了自己本地的S ...

  9. scrapy mid中间件一般处理方法

    import user_agent import requests class UA_midd(object): def process_request(self,request,spider): r ...

  10. flink RPC(akka)

    flink中的rpc框架使用的akka.在本节并不详细讲述akka,而是就flink中rpc来讲述akka的部分内容.本节,我从AkkaRpcActor.handleRpcInvocation方法讲起 ...