将Matplotlib输出保存到Databricks上的DBFS [英] Saving Matplotlib Output to DBFS on Databricks

查看:150
本文介绍了将Matplotlib输出保存到Databricks上的DBFS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Databricks上编写Python代码来处理一些数据和输出图。我希望能够将这些图形另存为图片文件(.png或某种形式,格式并不重要)到DBFS。

I'm writing Python code on Databricks to process some data and output graphs. I want to be able to save these graphs as a picture file (.png or something, the format doesn't really matter) to DBFS.

代码:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'fruits':['apple','banana'], 'count': [1,2]})
plt.close()
df.set_index('fruits',inplace = True)
df.plot.bar()
# plt.show()

我尝试过的事情:

plt.savefig("/FileStore/my-file.png")




[错误2]没有这样的文件或目录:'/FileStore/my-file.png'

[Errno 2] No such file or directory: '/FileStore/my-file.png'



fig = plt.gcf()
dbutils.fs.put("/dbfs/FileStore/my-file.png", fig)




TypeError:类型错误-预期为(,)。

TypeError: has the wrong type - (,) is expected.

经过研究,我认为fs.put仅在您使用要保存文本文件。

After some research, I think the fs.put only works if you want to save text files.

ru用 plt.show()上面的代码可以得到条形图-我希望能够将条形图作为图像保存到DBFS。

running the above code with plt.show() will get you a bar graph - I want to be able to save the bar graph as an image to DBFS. Any help is appreciated, thanks in advance!

推荐答案

使用matplotlib.pyplot更为简单。修复dbfs路径:

Easier way, just with matplotlib.pyplot. Fix the dbfs path:

示例

import matplotlib.pyplot as plt
plt.scatter(x=[1,2,3], y=[2,4,3])
plt.savefig('/dbfs/FileStore/figure.png')

这篇关于将Matplotlib输出保存到Databricks上的DBFS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆