Python代码直接生成图片方法

要让代码直接生成图片而不弹出浏览器窗口,你可以使用 Plotly 的 write_image 方法将图形直接保存为图片文件。以下是修改后的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import pandas as pd
import plotly.graph_objects as go
import numpy as np
import plotly.io as pio

# 读取 Excel 文件
file_path = 'data.xlsx' # 输入您的 Excel 文件路径
df = pd.read_excel(file_path, sheet_name='医生工作量统计') # 替换为您的表单名称

# 打印列名以确认
print("检查医师", df.columns)

# 检查并填充缺失值
df.fillna({'总数': 0}, inplace=True)

# 获取检查医师的唯一值,排除"汇总"
doctors = df[df['医师'] != '汇总']['医师'].unique()

# 创建图形
fig = go.Figure()

# 生成颜色
colors = [f'rgba({int(np.random.rand()*255)}, {int(np.random.rand()*255)}, {int(np.random.rand()*255)}, 0.7)' for _ in range(len(doctors))]

# 添加检查医师数据
for i, doctor in enumerate(doctors):
fig.add_trace(go.Bar(
x=[df[df['医师'] == doctor]['总数'].sum()], # 将病例数放在 x 轴
y=[str(doctor)], # 将医生名称放在 y 轴
name=str(doctor), # 确保将医生名称转换为字符串
marker=dict(color=colors[i]), # 使用生成的颜色
orientation='h' # 设置为横向柱状图
))

# 设置布局
fig.update_layout(
title='检查医师工作量统计',
title_x=0.5,
showlegend=True,
xaxis=dict(title='病例数'), # 现在 x 轴是病例数
yaxis=dict(title='检查医师'), # 现在 y 轴是检查医师
margin=dict(l=40, r=40, t=40, b=40),
height=600,
width=800
)

# 直接保存为图片而不显示
output_file = 'doctor_workload.png' # 输出文件名
pio.write_image(fig, output_file)
print(f"图片已保存为: {output_file}")

修改说明:

  1. 添加了 import plotly.io as pio 用于图片输出
  2. 删除了 fig.show() 以避免弹出浏览器窗口
  3. 使用 pio.write_image(fig, output_file) 直接将图形保存为图片
  4. 添加了输出提示信息

注意事项:

  • 需要安装 kaleido 包(Plotly 用于静态图片导出的引擎),可以通过 pip install kaleido 安装
  • 默认保存为 PNG 格式,你也可以通过修改文件扩展名保存为其他格式(如 .jpg, .svg, .pdf 等)
  • 如果不指定完整路径,图片会保存在当前工作目录下

如果你需要更高级的图片导出控制,可以参考 Plotly 的文档进一步自定义导出参数。

时间: 2025-06-09T02:39:22


👤 用户

下面这段代码运行后,会浏览器弹出一个窗口,显示一个报表的图形显示,我现在想运行这段代码,可以直接生成图片,而不需要弹出浏览器窗口然后点击下载图片,改这么修改?
import pandas as pd
import plotly.graph_objects as go
import numpy as np

读取 Excel 文件

file_path = ‘data.xlsx’ # 输入您的 Excel 文件路径
df = pd.read_excel(file_path, sheet_name=’医生工作量统计’) # 替换为您的表单名称

打印列名以确认

print(“检查医师”, df.columns)

检查并填充缺失值

df.fillna({‘总数’: 0}, inplace=True)

获取检查医师的唯一值,排除”汇总”

doctors = df[df[‘医师’] != ‘汇总’][‘医师’].unique()

创建图形

fig = go.Figure()

生成颜色

colors = [f’rgba({int(np.random.rand()*255)}, {int(np.random.rand()*255)}, {int(np.random.rand()*255)}, 0.7)’ for _ in range(len(doctors))]

添加检查医师数据

for i, doctor in enumerate(doctors):
fig.add_trace(go.Bar(
x=[df[df[‘医师’] == doctor][‘总数’].sum()], # 将病例数放在 x 轴
y=[str(doctor)], # 将医生名称放在 y 轴
name=str(doctor), # 确保将医生名称转换为字符串
marker=dict(color=colors[i]), # 使用生成的颜色
orientation=’h’ # 设置为横向柱状图
))

设置布局

fig.update_layout(
title=’检查医师工作量统计’,
title_x=0.5,
showlegend=True,
xaxis=dict(title=’病例数’), # 现在 x 轴是病例数
yaxis=dict(title=’检查医师’), # 现在 y 轴是检查医师
margin=dict(l=40, r=40, t=40, b=40),
height=600,
width=800
)

显示图形

fig.show()

时间: 2025-06-09T02:39:22


🤖 DeepSeek (deepseek-chat)

要让代码直接生成图片而不弹出浏览器窗口,你可以使用 Plotly 的 write_image 方法将图形直接保存为图片文件。以下是修改后的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import pandas as pd
import plotly.graph_objects as go
import numpy as np
import plotly.io as pio

# 读取 Excel 文件
file_path = 'data.xlsx' # 输入您的 Excel 文件路径
df = pd.read_excel(file_path, sheet_name='医生工作量统计') # 替换为您的表单名称

# 打印列名以确认
print("检查医师", df.columns)

# 检查并填充缺失值
df.fillna({'总数': 0}, inplace=True)

# 获取检查医师的唯一值,排除"汇总"
doctors = df[df['医师'] != '汇总']['医师'].unique()

# 创建图形
fig = go.Figure()

# 生成颜色
colors = [f'rgba({int(np.random.rand()*255)}, {int(np.random.rand()*255)}, {int(np.random.rand()*255)}, 0.7)' for _ in range(len(doctors))]

# 添加检查医师数据
for i, doctor in enumerate(doctors):
fig.add_trace(go.Bar(
x=[df[df['医师'] == doctor]['总数'].sum()], # 将病例数放在 x 轴
y=[str(doctor)], # 将医生名称放在 y 轴
name=str(doctor), # 确保将医生名称转换为字符串
marker=dict(color=colors[i]), # 使用生成的颜色
orientation='h' # 设置为横向柱状图
))

# 设置布局
fig.update_layout(
title='检查医师工作量统计',
title_x=0.5,
showlegend=True,
xaxis=dict(title='病例数'), # 现在 x 轴是病例数
yaxis=dict(title='检查医师'), # 现在 y 轴是检查医师
margin=dict(l=40, r=40, t=40, b=40),
height=600,
width=800
)

# 直接保存为图片而不显示
output_file = 'doctor_workload.png' # 输出文件名
pio.write_image(fig, output_file)
print(f"图片已保存为: {output_file}")

修改说明:

  1. 添加了 import plotly.io as pio 用于图片输出
  2. 删除了 fig.show() 以避免弹出浏览器窗口
  3. 使用 pio.write_image(fig, output_file) 直接将图形保存为图片
  4. 添加了输出提示信息

注意事项:

  • 需要安装 kaleido 包(Plotly 用于静态图片导出的引擎),可以通过 pip install kaleido 安装
  • 默认保存为 PNG 格式,你也可以通过修改文件扩展名保存为其他格式(如 .jpg, .svg, .pdf 等)
  • 如果不指定完整路径,图片会保存在当前工作目录下

如果你需要更高级的图片导出控制,可以参考 Plotly 的文档进一步自定义导出参数。

时间: 2025-06-09T02:39:22



对话统计

  • 消息总数: 2 条
  • 处理状态: 成功转换