python pyplot画图转化为html

2023-12-29 16:51:26

优点:画图转化为html可以手动拖动。并且可以放大缩小
示例

import matplotlib.pyplot as plt
import mpld3

# 准备数据和图表
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]

fig, ax = plt.subplots(figsize = (10,10))
ax.plot(x, y, 'o-', label='Data Points')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_title('Interactive Matplotlib Plot')
ax.legend()

# 使用 mpld3 将图表转换为 HTML
html_content = mpld3.fig_to_html(fig)

# 将 HTML 保存到文件
output_html_path = 'output.html'
with open(output_html_path, 'w', encoding='utf-8') as html_file:
    html_file.write(html_content)

print(f"Interactive Matplotlib plot saved successfully. Open the following file in your web browser: {output_html_path}")

文章来源:https://blog.csdn.net/m0_56306892/article/details/135291774
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。