python 画图转化为html

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

示例二

import plotly.graph_objects as go

# 创建一组数据
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]


fig = go.Figure(data=go.Scatter(x=x, y=y))
fig.update_layout(title='示例交互式折线图', xaxis_title='X轴', yaxis_title='Y轴')
fig.write_html('plot.html')  # 将图表保存为 HTML 文件

示例三

fig = go.Figure()
for sin in target_singnal:
	fig.add_trace(go.Scatter(x=np.arange(len(_y)), y=slider_dtw(AssctdBin_df[sin], NativeBin_df[sin], size_step), mode='lines+markers', name="NativeBin_{0}".format(sin)))
fig.update_layout(title='示例柱状图', xaxis_title='X轴', yaxis_title='Y轴')
fig.write_html(fload_base + "/" + sin  + "bar_chart_err.html", full_html=False, include_plotlyjs='cdn')
  • 8
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值