折线图

下面是绘制折线图,设置图片的横轴纵轴标签,图片标题的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. ECMA5中定义的对象属性特性和方法

    ECMA5规定了只有内部才有的特性,描述了属性的各种特征,这些特性用于实现JavaScript引擎,因此在Js中不能直接访问他们.为了标识特性,我们一般会他们放入两对方括号中. ECMAScript中 ...

  2. MySQL基础篇(05):逻辑架构图解和InnoDB存储引擎详解

    本文源码:GitHub·点这里 || GitEE·点这里 一.MySQL逻辑架构 1.逻辑架构图 基于下面的逻辑架构图,可以大致熟悉MySQL各个架构组件之间的协同工作关系. 很经典的C/S架构风格, ...

  3. 如何理解js中的this和实际应用中需要避开哪些坑

    this是什么 this就是函数内部的关键字 看下面例子理解js中的this // 例子1 function fnOne () { console.log(this) } 'use strict' f ...

  4. oracle根据特定字符拆分字符串的方法

    清洗数据需要将某个字段内以空格分隔的字符串拆分成多行单个的字符串,百度了很多种方法大概归结起来也就这几种方法最为有效,现在把贴出来: 第一种: select regexp_substr('1 2 3' ...

  5. Chromium的无锁线程模型C++代码示例

    引言 作者:程序员bingo,主要关注客户端架构设计.性能优化.崩溃处理,有多年的Chromium浏览器开发经验. 多线程一直是软件开发中最容易出问题的环节,很多的崩溃.卡死问题都与多线程有关.在常用 ...

  6. Simulink仿真入门到精通(十四) Simulink自定义环境

    14.1 Simulink环境自定义功能 sl_sustomization.m函数是Simulink提供给用户使用MATLAB语言自定义Simulink标准人机界面的函数机制.若sl_sustomiz ...

  7. BigDecimal介绍及BigDecimal实现四舍五入

    BigDecimal介绍及BigDecimal实现四舍五入 BigDecimal是什么? 我们知道float最大精度是7-8位有效数字,而double的最大精度是16-17位有效数字,那么大于16位的 ...

  8. setTimeout和setImmediate到底谁先执行,本文让你彻底理解Event Loop

    笔者以前面试的时候经常遇到写一堆setTimeout,setImmediate来问哪个先执行.本文主要就是来讲这个问题的,但是不是简单的讲讲哪个先,哪个后.笼统的知道setImmediate比setT ...

  9. 妈妈再也不担心我面试被Redis问得脸都绿了

    长文前排提醒,收藏向前排提醒,素质三连 (转发 + 在看 + 留言) 前排提醒! 前言 Redis 作为一个开源的,高级的键值存储和一个适用的解决方案,已经越来越在构建 「高性能」.「可扩展」 的 W ...

  10. eclipse 配置tomcat运行环境

    eclipse环境下如何配置tomcat,并且把项目部署到Tomcat服务器上 1.打开Eclipse,单击“Window”菜单,选择下方的“Preferences”. 2.单击“Server”选项, ...