如何将jupyter里plt.pie画出的图保存到桌面
时间: 2023-06-04 07:09:02 浏览: 667
您可以使用以下代码将jupyter中绘制的饼图保存到桌面:
```python
import matplotlib.pyplot as plt
# 绘制饼图
data = [25, 30, 45]
plt.pie(data)
plt.show()
# 保存饼图
plt.savefig('C:/Users/<username>/Desktop/pie_chart.png', dpi=300, bbox_inches='tight')
```
请注意将`<username>`替换为您的电脑用户名。保存的文件将出现在您的桌面上,文件名为`pie_chart.png`。
相关问题
如何将jupyter里plt.pie画出的多张图保存到桌面
您可以使用以下代码将jupyter中plt.pie画出的多张图保存到桌面:
```
import matplotlib.pyplot as plt
import os
# 在代码中需要保存的路径中创建一个新的文件夹
save_dir = os.path.expanduser("~/Desktop/my_pie_charts")
os.makedirs(save_dir, exist_ok=True)
# 创建一个loop,以便在plt.pie返回多个图像时进行保存
# 根据您的需求分配变量,例如数据列表和标签列表
# 使用循环按顺序保存每个图像
for i in range(3):
plt.pie(data_list[i], labels=label_list[i])
plt.savefig(os.path.join(save_dir, f"my_pie_chart_{i}.png"))
plt.clf()
```
将上述代码复制到jupyter文件中并运行,它将在您的桌面上创建一个新文件夹,并将您的饼图保存为my_pie_chart_0.png,my_pie_chart_1.png和my_pie_chart_2.png。您可以根据需要更改文件名和文件类型,例如.jpg或.gif。
如何将jupyter里plt.pie画出的多张图分别保存到桌面
您可以使用以下代码将jupyter中的多个plt.pie生成的图分别保存到桌面:
```python
import matplotlib.pyplot as plt
import os
# 生成多张图
for i in range(num_of_pie_charts):
# 创建每张图的数据和标签
data = ...
labels = ...
# 生成饼图
plt.pie(data, labels=labels)
# 保存饼图
filename = f"pie_chart_{i}.png"
desktop_path = os.path.expanduser("~/Desktop")
filepath = os.path.join(desktop_path, filename)
plt.savefig(filepath)
# 关闭当前图
plt.close()
```
其中,`num_of_pie_charts`表示你要生成多少张饼图,`data`和`labels`分别为每张图的数据和标签。`os.path.expanduser("~/Desktop")`表示获取桌面路径,`plt.savefig(filepath)`将当前图保存为指定路径下的文件,`plt.close()`关闭当前图,以便生成下一张饼图。
阅读全文
相关推荐













