首页 前端知识 python 画图转化为html

python 画图转化为html

2024-06-04 10:06:17 前端知识 前端哥 295 831 我要收藏

优点:画图转化为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')
转载请注明出处或者链接地址:https://www.qianduange.cn//article/10761.html
标签
评论
发布的文章

js-jquery页面跳转集合

2024-06-08 22:06:13

echarts柱状图数据过多

2024-06-08 22:06:31

大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!