python绘制散点图、如何修改属性_python – 如何为散点图设置动画?
假设你有一个散点图,scat = ax.scatter(…),那么你可以
>改变立场
scat.set_offsets(array)
其中数组是x和y坐标的N x 2形数组.
>改变大小
scat.set_sizes(array)
其中array是一维大小的1D数组.
>改变颜色
scat.set_array(array)
其中array是将进行颜色映射的一维值数组.
这是使用animation module的简单示例.
它稍微复杂一点,但这应该给你一个框架来做更好的事情.
(2019年4月编辑的代码与当前版本兼容.旧代码见revision history)
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
class AnimatedScatter(object):
"""An animated scatter plot using matplotlib.animations.FuncAnimation."""
def __init__(self, numpoints=50):
self.numpoints = numpoints
self.stream = self.data_stream()
# Setup the figure and axes...
self.fig, self.ax = plt.subplots()
# Then setup FuncAnimation.
self.ani = animation.FuncAnimation(self.fig, self.update, interval=5,
init_func=self.setup_plot, blit=True)
def setup_plot(self):
"""Initial drawing of the scatter plot."""
x, y, s, c = next(self.stream).T
self.scat = self.ax.scatter(x, y, c=c, s=s, vmin=0, vmax=1,
cmap="jet", edgecolor="k")
self.ax.axis([-10, 10, -10, 10])
# For FuncAnimation's sake, we need to return the artist we'll be using
# Note that it expects a sequence of artists, thus the trailing comma.
return self.scat,
def data_stream(self):
"""Generate a random walk (brownian motion). Data is scaled to produce
a soft "flickering" effect."""
xy = (np.random.random((self.numpoints, 2))-0.5)*10
s, c = np.random.random((self.numpoints, 2)).T
while True:
xy += 0.03 * (np.random.random((self.numpoints, 2)) - 0.5)
s += 0.05 * (np.random.random(self.numpoints) - 0.5)
c += 0.02 * (np.random.random(self.numpoints) - 0.5)
yield np.c_[xy[:,0], xy[:,1], s, c]
def update(self, i):
"""Update the scatter plot."""
data = next(self.stream)
# Set x and y data...
self.scat.set_offsets(data[:, :2])
# Set sizes...
self.scat.set_sizes(300 * abs(data[:, 2])**1.5 + 100)
# Set colors..
self.scat.set_array(data[:, 3])
# We need to return the updated artist for FuncAnimation to draw..
# Note that it expects a sequence of artists, thus the trailing comma.
return self.scat,
if __name__ == '__main__':
a = AnimatedScatter()
plt.show()
如果您使用OSX并使用OSX后端,则需要在下面的FuncAnimation初始化中将blit = True更改为blit = False. OSX后端不完全支持blitting.性能将受到影响,但该示例应该在OSX上正确运行并禁用blit.
有关更新颜色的更简单示例,请查看以下内容:
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
def main():
numframes = 100
numpoints = 10
color_data = np.random.random((numframes, numpoints))
x, y, c = np.random.random((3, numpoints))
fig = plt.figure()
scat = plt.scatter(x, y, c=c, s=100)
ani = animation.FuncAnimation(fig, update_plot, frames=xrange(numframes),
fargs=(color_data, scat))
plt.show()
def update_plot(i, data, scat):
scat.set_array(data[i])
return scat,
main()
python绘制散点图、如何修改属性_python – 如何为散点图设置动画?相关推荐
- python绘制条形图用什么函数_Python绘制正余弦函数图像完整代码
通过python绘制正弦和余弦函数,从默认的设置开始,一步一步地调整改进,让它变得好看,变成我们初高中学习过的图象那样.通过这个过程来学习如何进行对图表的一些元素的进行调整. 01. 简单绘图 mat ...
- python绘制彩色地震剖面断层解释_python绘制地震散点图
本项目是利用五年左右的世界地震数据,通过python的pandas库.matplotlib库.basemap库等进行数据可视化,绘制出地震散点图.主要代码如下所示 from __future__ im ...
- python解析xml读取指定属性_python批量修改xml某些内容和属性
在训练时,有时候需要将jpg格式转换为png格式,顺便也要把jpg标注的xml文件转换为png的xml文件.例如,需要把xml文件中的图像后缀名".jpg"批量去掉,这时候就需要批 ...
- python描述符与实例属性_Python 中的属性访问与描述符
在Python中,对于一个对象的属性访问,我们一般采用的是点(.)属性运算符进行操作.例如,有一个类实例对象foo,它有一个name属性,那便可以使用foo.name对此属性进行访问.一般而言,点(. ...
- python绘制条形图用什么函数_python绘制条形图方法代码详解
1.首先要绘制一个简单的条形图 import numpy as np import matplotlib.pyplot as plt from matplotlib import mlab from ...
- python绘制三维地形shade()参数_python中的Matplot库和Gdal库绘制富士山三维地形图-参考了虾神的喜马拉雅山...
首先请大家读一下面这篇文章了解什么是Gdal http://blog.csdn.net/grllery/article/details/77822595 剩下的我要公布绘制富士山的代码了,虽然基本co ...
- python对象的三个属性_Python 对象属性的访问
在 Python 中,一切皆对象.属性访问可以理解为是从一个已有的对象中获得另一个对象的方法.对象属性的访问涉及到对象的 __dict__ 属性.描述符等概念,以及 __getattribute__. ...
- python中plot函数的属性_Python matplotlib 学习-绘图函数
1 所使用函数说明 (1) plot函数 plt.plot(x,y,ls='--',lw =2,label='text') 参数说明 ● x:x轴上的数值. ● y:y轴上的数值. ● ls:折线图的 ...
- python绘制简单城市剪影图_Python之绘制个人足迹地图
前两年,足迹地图小程序风靡朋友圈,一时间大家都流行晒自己的旅行地图.但是,笔者最近体验了好几款足迹地图的小程序,发现这些小程序虽然号称是足迹地图,但最多只是展示到省级别,无法精确到市级别,因此,笔者周 ...
最新文章
- android google 下拉刷新 csdn,android SwipeRefreshLayout google官方下拉刷新控件
- AC日记——[HNOI2010]BOUNCE 弹飞绵羊 洛谷 P3203
- 成绩从大到小排序c语言,刚学c语言,老师让用if编一个五个数字从大到小的排序,有那个大神能帮我,谢谢啦...
- mysql数据库链接百分号_数据库百分号怎么用
- js检测数据类型的方法你都掌握了几个?
- 如何一秒钟从头构建一个 ASP.NET Core 中间件
- UI自动化之读取浏览器配置
- 笔记:Hadoop权威指南 第1章 初识Hadoop
- c语言经典程序100txt例,C语言经典程序100例txt格式.doc
- body curl命令测试post_HTTP接口调试利器!4.8万Star的HTTP命令行客户端!
- 计算机网络技术-----==一些东西
- 英语读音(一) English Pronounciation
- IT狂人第一至四季/全集The IT Crowd迅雷下载
- 莫以物喜 、莫以己悲!
- Python wheel文件
- 布兰妮新年演唱会上用狗项圈“遛”男友
- 切片 (python)
- wps在线预览接口_金山文档在线编辑 - 快速接入 - 《WPS开放平台技术文档》 - 书栈网 · BookStack...
- 第六章 网络编程——SOCKET 开发
- 《算法设计与分析》部分算法总结