Python画图|极坐标下的散点图动态输出

【1】引言

前序已经学习过散点图输出和极坐标图输出,文章链接包括但不限于下述部分:

python画散点图|scatter()函数小试牛刀(入门级教程)_python ax.scatter-CSDN博客

python画图|极坐标中画散点图_极坐标上的散点图-CSDN博客

【2】自主探索

在此基础上,我自主探索了极坐标下的散点图输出。

相关代码见:

python 复制代码
import matplotlib.pyplot as plt #引入画图模块
import numpy as np #引入计算模块

import matplotlib.animation as animation #引入动画模块

fig = plt.figure() #定义要画图
ax = fig.add_subplot(projection='polar') #定义要画极坐标图
#ax.set_xlim([0, 10])

scatters = ax.scatter(0, 2,5) #定义初始散点
x = np.linspace(0, 10,100) #定义自变量


def animate(i): #自定义动画函数
    ax.scatter(x[i], x[i]**2, 12*x[i]) #极坐标中的散点的坐标点(x[i], x[i]**2),散点大小12*x[i]
    return scatters,

ani = animation.FuncAnimation(fig, animate) #输出动画

# To save the animation using Pillow as a gif
# writer = animation.PillowWriter(fps=15,
#                                 metadata=dict(artist='Me'),
#                                 bitrate=1800)
ani.save('polar-scatter.gif') #保存动画

plt.show() #输出图形

++图1++

【3】代码解读

具体的代码书写步骤为:

首先引入画图、计算和动画模块:

复制代码
import matplotlib.pyplot as plt #引入画图模块
import numpy as np #引入计算模块

import matplotlib.animation as animation #引入动画模块

然后定义画图,确认画极坐标图:

复制代码
fig = plt.figure() #定义要画图
ax = fig.add_subplot(projection='polar') #定义要画极坐标图

之后给出初始散点和自变量:

复制代码
scatters = ax.scatter(0, 2,5) #定义初始散点
x = np.linspace(0, 10,60) #定义自变量

然后定义一个动画函数:

复制代码
def animate(i): #自定义动画函数
    ax.scatter(x[i], x[i]**2, 120*x[i],cmap='blues_r') #极坐标中的散点的坐标点(x[i], x[i]**2),散点大小12*x[i]
    return scatters,

最后调用动画模块输出图形:

复制代码
ani = animation.FuncAnimation(fig, animate) #输出动画

# To save the animation using Pillow as a gif
# writer = animation.PillowWriter(fps=15,
#                                 metadata=dict(artist='Me'),
#                                 bitrate=1800)
ani.save('polar-scatter.gif') #保存动画

plt.show() #输出图形

【4】代码优化

由于极坐标下,一个完整的圆周对应角度是360度即2π,因此将自变量的值设置为[0,6.28]即可:

复制代码
x = np.linspace(0, 6.28,100) #定义自变量

然后为了多输出几个图形,定义动画函数为:

复制代码
def animate(i): #自定义动画函数
    ax.scatter(x[i],np.cos(x[i])) #弦长输出余弦
    ax.scatter(x[i], np.sin(x[i])) #弦长输出正弦
    ax.scatter(x[i],-np.cos(x[i])) #弦长输出余弦取负值
    return scatters,

此时运行代码的输出图像为:

图2

【5】代码完善

给极坐标的动图增加一个图名:

复制代码
ax.set_title('polar-scatter-aixmls')

++图3++

【6】总结

自主探索了极坐标散点动图绘制技巧。

相关推荐
SizeTheMoment14 天前
List介绍
1024程序员节
开利网络16 天前
产业互联网+三融战略:重构企业增长密码
大数据·运维·服务器·人工智能·重构·1024程序员节
wei_shuo23 天前
从数据中台到数据飞轮:实现数据驱动的升级之路
1024程序员节·数据飞轮
玖剹1 个月前
矩阵区域和 --- 前缀和
数据结构·c++·算法·leetcode·矩阵·动态规划·1024程序员节
jamison_12 个月前
文心一言与 DeepSeek 的竞争分析:技术先发优势为何未能转化为市场主导地位?
人工智能·ai·chatgpt·gpt-3·1024程序员节
NaZiMeKiY2 个月前
HTML5前端第六章节
前端·html·html5·1024程序员节
jamison_12 个月前
颠覆未来:解锁ChatGPT衍生应用的无限可能(具体应用、功能、付费模式与使用情况)
ai·chatgpt·1024程序员节
NaZiMeKiY3 个月前
HTML5前端第七章节
1024程序员节
earthzhang20213 个月前
《Python深度学习》第四讲:计算机视觉中的深度学习
人工智能·python·深度学习·算法·计算机视觉·numpy·1024程序员节
明明真系叻3 个月前
2025.3.2机器学习笔记:PINN文献阅读
人工智能·笔记·深度学习·机器学习·1024程序员节·pinn