网站首页 > 开源技术 正文
(matplotlib,matplotlib.animation,pyechart,echart)
方法一:matplotlib循环添加数据
方法二:matplotlib循环刷新-清除旧数据-添加新数据
方法三、利用第三方插件imagemagick实现动态图保存gif文件
方法四:利用pyecharts实现图展示-保存为html
方法五:利用flask和pyechart组合-web发布图形
方法一:matplotlib循环添加数据
import numpy as np
import matplotlib.pyplot as plt
"""
动态绘图方法一:
通过刷新图面的方法,每次循环在绘制新图画前,把当前绘图区的内容进行清空,
然后绘制新的图形
"""
fig=plt.figure() #设置图面大小
plt1=plt.subplot(211) #设置绘图区域2行1列,第一个图区
plt2=plt.subplot(212)#设置绘图区域2行1列,第二个图区
plt1.axis([0, 100, 0, 1])
xa=[]
ya = []
pause_time=0.01 #动态刷新时间
for i in range(50):
y = np.random.random()
ya.append(y) # 每迭代一次,将i放入y1中画出来
xa.append(i)
plt1.cla() # 清除键
plt1.plot(xa,ya)
plt.pause(pause_time)
方法二:matplotlib循环刷新-清除旧数据-添加新数据
"""
利用绘图的特性,每次绘制的内容,在前一次的结果上添加,
这个方法需要对数据进行特殊处理,每次绘制的数据,只有新添加的数据,旧的数据需要删除
"""
plt2.axis([0, 100, 0, 1])
xs = [0, 0]
ys = [1, 1]
for i in range(50):
y = np.random.random()
xs[0] = xs[1]
ys[0] = ys[1]
xs[1] = i
ys[1] = y
plt2.plot(xs, ys)
plt.pause(pause_time)
plt.show()
方法三、利用第三方插件imagemagick实现动态图保存gif文件
1.首先现在imagemagick软件,这里有个坑,需要下载6.9版本的,应为7.0版本没有了convert.exe命令,必须用6.9版本的,网上的教程都是用的这个convert命令
2.安装完,imagemagick后,matplotlib并不知道这个命令所在的位置,因此需要告诉命令convert的绝对位置
在python中利用print(matplotlib.matplotlib_fname()),获得matplotlib配置文件的位置,在最后一行,修改命令的全局路径,为animation.convert_path:D:\ProgramFiles\ImageMagick-6.9.10-Q16\convert.exe
3.在程序中就可以正常使用了
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation
print(matplotlib.matplotlib_fname()) # 修改matplotlib配置文件的位置
fig = plt.figure() # 画纸
ax = plt.subplot() # 绘图区
xdata, ydata = [], [] # x,y轴数据数组
ln, = plt.plot([], [], 'r', animated=True) #
def init():
ax.set_xlim(0, 100)
ax.set_ylim(0, 1)
return ln,
def update(frame): #frame的数据来自FuncAnimation函数frams的内容,每次调用函数,区frames中的一个数据
xdata.append(frame)
# print(frame)
ydata.append(np.random.random())
ln.set_data(xdata, ydata) #在ln中添加数据
return ln,
anim = animation.FuncAnimation(fig, update, frames=range(10,60), interval=10, init_func=init, blit=True, repeat=False)
# anim.save('sinx.gif', writer='imagemagick') #存储为gif文件
# anim.save('sinx2.html') #利用默认的工具,存储为html文件
plt.show()
方法四:利用pyecharts实现图展示-保存为html
1.利用echart进行实现,利用网页形式,在web前端显示图形,让后利用echart获得后台的数据
2.需要工具,echart,flask
3.pip install echarts
4.保存为gif
① 1.如果想直接将图片保存为 png, pdf, gif 格式的文件,可以使用 pyecharts-snapshot。使用该插件请确保你的系统上已经安装了 Nodejs 环境。
② 安装 phantomjs $ npm install -g phantomjs-prebuilt
③ 安装 pyecharts-snapshot $ pip install pyecharts-snapshot
④ 调用 render 方法 bar.render(path='snapshot.png') 文件结尾可以为 svg/jpeg/png/pdf/gif。请注意,svg 文件需要你在初始化 bar 的时候设置 renderer='svg'。
5. 首先利用pyecharts画一个静态的图形
#http://pyecharts.org/#/zh-cn/prepare 帮助文件
from pyecharts import Line
import numpy as np
x=list(range(0,50))
y1=list(np.random.random(50))
y2=list(np.random.random(50))
y2=[m/2 for m in y2 if m>0] #for的迭代式式,加过滤器if,生产list
line=Line("折线图")
line.add("A",x,y1,mark_point=["average"],line_width=3,line_color="red")line.add("B",x,y2,mark_point=["average"],line_width=3,line_color="blue")#line.show_config()
line.render("myechart001.html") #生成html文件
import webbrowser
webbrowser.open("myechart001.html") #
方法五:利用flask和pyechart组合-web发布图形
① 首先要安装flask框架
② 在windows的命令行执行:python -m venv myenv
③ 进入虚拟环境命令的脚本文件夹,激活虚拟环境
④ 然后安装flask包
⑤ 建立html模板目录结构
两个文件的代码如下
python程序如下
import random
from pyecharts import Scatter3D
from flask import Flask, render_template
app = Flask(__name__)
REMOTE_HOST = "https://pyecharts.github.io/assets/js"
@app.route("/")
def hello():
s3d = scatter3d()
return render_template(
"pyecharts.html",
myechart=s3d.render_embed(),
host=REMOTE_HOST,
script_list=s3d.get_js_dependencies(),
)
def scatter3d():
data = [generate_3d_random_point() for _ in range(80)]
range_color = [
"#313695",
"#4575b4",
"#74add1",
"#abd9e9",
"#e0f3f8",
"#fee090",
"#fdae61",
"#f46d43",
"#d73027",
"#a50026",
]
scatter3D = Scatter3D("3D scattering plot demo", width=1200, height=600)
scatter3D.add("", data, is_visualmap=True, visual_range_color=range_color)
return scatter3D
def generate_3d_random_point():
return [
random.randint(0, 100), random.randint(0, 100), random.randint(0, 100)
]
if __name__=='__main__':
app.run(debug=True)
html文件的内容如下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Proudly presented by ECharts</title>
{% for jsfile_name in script_list %}
<script src="{{ host }}/{{ jsfile_name }}.js"></script>
{% endfor %}
</head>
<body>
{{ myechart|safe }}
</body>
</html>
⑥ 在pycharm中运行server.py文件,在浏览器中访问127.0.0.1:5000即可
这是官方的例子。
在此基础上修改为折线图
from pyecharts import Line #引入不同的图形模块
from flask import Flask, render_template
import numpy as np
app = Flask(__name__)
REMOTE_HOST = "https://pyecharts.github.io/assets/js" #远程js库,可以下载全部到自己的服务器上,修改这个路径就可以了
@app.route("/")
def hello():
myline=mydata_line() #这个地方就是基本的pyechart图形绘制,返回一个图形对象
return render_template(
"pyecharts.html", #模板的名称,模板默认位置在templates文件夹下
myechart=myline.render_embed(), #给模板传递echart图形参数
host=REMOTE_HOST, #js库的位置
script_list=myline.get_js_dependencies(), #给模块传echart图形的js代码,我理解的,不一定对
)
#此函数就是普通的画图函数,数据怎么处理都可以,利用基本知识
def mydata_line():
x=list(range(50))
y1=list(np.random.random(50))
y2=list(np.random.random(50))
line=Line("折线图flask")
line.add("A",x,y1)
line.add("B",x,y2)
#line.render("myechart001.html") # 生成html文件,这是静态方法生成网页
return line
if __name__=='__main__':
app.run(debug=True)
html文件的内容不变,还利用官方例子的内容。
这种方法,比较简单可以快速的发布html格式的网页,但是问题是html网页的内容比较单一,需要根据需求进行修改,但是应用的案例比较少,属于小众方法。
主要问题:没有方法实现动态刷新图形图形的方法,图形都是利用已知的数据,生成静态的html网页进行发布,因此不适用动态刷新的方法
扩展思路,直接利用底层得echart进行数据展示
猜你喜欢
- 2024-11-17 Python动态绘图的方法
- 2024-11-17 AI数据分析:用kimi生成一个正弦波数学动画
- 2024-11-17 如何把python绘制的动态图形保存为gif文件或视频
- 2024-11-17 Java 图片压缩生成缩略图和水印
- 2024-11-17 医疗影像工具LEADTOOLS 入门教程: 使用文档编写器创建文档 - C#
- 2024-11-17 Celluloid让matplotlib动画-2:红绿灯
- 2024-11-17 使用Adobe dng SDK一步一步显示图像
- 2024-11-17 方便!Python 操作 Excel 神器 xlsxwriter 初识
- 2024-11-17 image 用 Rust 编写的图像库——001号RUST库
- 2024-11-17 Qt开源作品11-屏幕录制控件
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- jdk (81)
- putty (66)
- rufus (78)
- 内网穿透 (89)
- okhttp (70)
- powertoys (74)
- windowsterminal (81)
- netcat (65)
- ghostscript (65)
- veracrypt (65)
- asp.netcore (70)
- wrk (67)
- aspose.words (80)
- itk (80)
- ajaxfileupload.js (66)
- sqlhelper (67)
- express.js (67)
- phpmailer (67)
- xjar (70)
- redisclient (78)
- wakeonlan (66)
- tinygo (85)
- startbbs (72)
- webftp (82)
- vsvim (79)
本文暂时没有评论,来添加一个吧(●'◡'●)