Python之Matplotlib创作

简介: Python之Matplotlib创作

2.1 准备背景#
在绘制动画前,我们需将其sin函数的背景绘制出来。这一步很简单,与我们平时的绘图一样。

Copy Highlighter-hljs
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation

指定渲染环境

%matplotlib notebook

%matplotlib inline

x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)

fig = plt.figure(tight_layout=True)
plt.plot(x,y)
plt.grid(ls="--")
plt.show()
fig-sin_bg fig2-2-sin_test
图2-1 sin背景图 图2-2 sin函数点运动效果1
2.2 往背景中添加动画点#
这一步代码稍微多了一点点,且先看代码,再来解释。

Copy Highlighter-hljs
def update_points(num):
//代码效果参考:https://v.youku.com/v_show/id_XNjQwNjg0MjYyMA==.html

'''
更新数据点
'''
point_ani.set_data(x[num], y[num])
return point_ani,

x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)

fig = plt.figure(tight_layout=True)
plt.plot(x,y)
point_ani, = plt.plot(x[0], y[0], "ro")
plt.grid(ls="--")

开始制作动画

ani = animation.FuncAnimation(fig, update_points, np.arange(0, 100), interval=100, blit=True)

ani.save('sin_test2.gif', writer='imagemagick', fps=10)

plt.show()
上面的代码中,首先定义了一个update_points函数,用于更新绘制的图中的数据点。此函数的输入参数num代表当前动画的第几帧,函数的返回,即为我们需要更新的对象,需要特别注意的是:reuturn point_ani,这个逗号一定加上,否则动画不能正常显示。当然这里面操作的点对象point_ani我们一般会提前声明得到:point_ani, = plt.plot(x[0], y[0], "ro")。接下来就是将此函数传入我们的FuncAnimation函数中,函数的参数说明可以参见官网,这里简要说明用到的几个参数。

第1个参数fig:即为我们的绘图对象.
第2个参数update_points:更新动画的函数.
第3个参数np.arrange(0, 100):动画帧数,这需要是一个可迭代的对象。
interval参数:动画的时间间隔。
blit参数:是否开启某种动画的渲染。
运行上面代码可以得到如图2-2所示的动画效果。

2.3 往动画中添加其它效果#
上面实现的动画效果还比较单一,我们可以往其中添加一些文本显示,或者在不同的条件下改变点样式。这其实也非常简单,只需在update_points函数中添加一些额外的,你想要的效果代码即可。

Copy Highlighter-hljs
def update_points(num):
if num%5==0:
point_ani.set_marker("*")
point_ani.set_markersize(12)
else:
point_ani.set_marker("o")
point_ani.set_markersize(8)

point_ani.set_data(x[num], y[num])    
text_pt.set_text("x=%.3f, y=%.3f"%(x[num], y[num]))
return point_ani,text_pt,

x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)

fig = plt.figure(tight_layout=True)
plt.plot(x,y)
point_ani, = plt.plot(x[0], y[0], "ro")
plt.grid(ls="--")
text_pt = plt.text(4, 0.8, '', fontsize=16)

ani = animation.FuncAnimation(fig, update_points, np.arange(0, 100), interval=100, blit=True)

ani.save('sin_test3.gif', writer='imagemagick', fps=10)

plt.show()
我在上面update_points函数中添加了一个文本,让它显示点的
(
x
,

y
)
的坐标值,同时在不同的帧,改变了点的形状,让它在5的倍数帧显示为五角星形状。

fig-sin_test1 fig-sin_test3
图2-3 sin函数点运动效果2 图2-4 sin函数点运动效果3
再稍微改变一下,可以让文本跟着点动。只需将上面的代码update_points函数改为如下代码,其效果如图2-4所示。

Copy Highlighter-hljs
def update_points(num):

point_ani.set_data(x[num], y[num])
if num%5==0:

//代码效果参考:https://v.youku.com/v_show/id_XNjQwNjgzOTQwOA==.html
point_ani.set_marker("*")
point_ani.set_markersize(12)
else:
point_ani.set_marker("o")
point_ani.set_markersize(8)

text_pt.set_position((x[num], y[num]))
text_pt.set_text("x=%.3f, y=%.3f"%(x[num], y[num]))
return point_ani,text_pt,
相关文章
|
17天前
|
数据可视化 Python Windows
使用Python进行数据可视化(一、matplotlib)
使用Python进行数据可视化(一、matplotlib)
|
18天前
|
数据采集 数据可视化 数据挖掘
数据分析大神养成记:Python+Pandas+Matplotlib助你飞跃!
【6月更文挑战第12天】在数字时代,Python因其强大的数据处理能力和易用性成为数据分析首选工具。结合Pandas(用于高效数据处理)和Matplotlib(用于数据可视化),能助你成为数据分析专家。Python处理数据预处理、分析和可视化,Pandas的DataFrame简化表格数据操作,Matplotlib则提供丰富图表展示数据。掌握这三个库,数据分析之路将更加畅通无阻。
|
3天前
|
Python
Python之Matplotlib创作
Python之Matplotlib创作
|
8天前
|
数据可视化 数据挖掘 API
Python数据可视化基础:使用Matplotlib绘制图表
Python的Matplotlib是数据可视化的首选库,它提供静态、动态和交互式图表。要开始,先通过`pip install matplotlib`安装。绘制基本折线图涉及导入`pyplot`,设定数据,然后用`plot()`函数画图,如: ```markdown import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 11] plt.plot(x, y, 'o') plt.show() ``` 自定义图表包括更改线条样式、颜色等,例如: ```markdown
|
15天前
|
Python
Python学习笔记之Matplotlib模块入门(直线图、折线图、曲线图、散点图、柱状图、饼状图、直方图、等高线图和三维图的绘制)-2
Python学习笔记之Matplotlib模块入门(直线图、折线图、曲线图、散点图、柱状图、饼状图、直方图、等高线图和三维图的绘制)
|
15天前
|
数据可视化 开发者 Python
Python学习笔记之Matplotlib模块入门(直线图、折线图、曲线图、散点图、柱状图、饼状图、直方图、等高线图和三维图的绘制)-1
Python学习笔记之Matplotlib模块入门(直线图、折线图、曲线图、散点图、柱状图、饼状图、直方图、等高线图和三维图的绘制)
|
20天前
|
人工智能 数据可视化 开发者
|
1月前
|
数据可视化 数据挖掘 Python
【Python DataFrame专栏】DataFrame的可视化探索:使用matplotlib和seaborn
【5月更文挑战第20天】本文介绍了使用Python的pandas、matplotlib和seaborn库进行数据可视化的步骤,包括创建示例数据集、绘制折线图、柱状图、散点图、热力图、箱线图、小提琴图和饼图。这些图表有助于直观理解数据分布、关系和趋势,适用于数据分析中的探索性研究。
【Python DataFrame专栏】DataFrame的可视化探索:使用matplotlib和seaborn
|
21天前
|
数据可视化 数据挖掘 大数据
Python中的数据可视化库Matplotlib及其应用
数据可视化是数据分析过程中至关重要的一环,而Matplotlib作为Python中最流行的数据可视化库之一,为用户提供了丰富的绘图工具和定制选项。本文将介绍Matplotlib的基本用法和常见应用,帮助读者更好地利用这一强大工具进行数据呈现和分析。
|
1月前
|
数据可视化 BI 索引
【Python】—— matplotlib数据可视化
【Python】—— matplotlib数据可视化
29 1