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')

你可能感兴趣的:(python,html,开发语言)