优点:画图转化为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}")