【问题标题】:Saving matplotlib figure on azure blob storage在 azure blob 存储上保存 matplotlib 图
【发布时间】:2019-12-24 17:04:36
【问题描述】:

我有一个使用 WordCloud 类创建的词云。我正在使用 matplotlib 绘制这个词云。现在我想将这个数字保存在 azure blob 存储中,但我找不到任何 python SDK 来做同样的事情。

为了使用 *plt.savefig(),需要 blob 存储路径。谁能告诉如何提及这条路径或以其他方式将其存储在 blob 上?

我使用的代码是:

fig, ax = plt.subplots()
words = text.split()
word_cloud = WordCloud(width = 8000, height = 800, 
                background_color ='black', 
                min_font_size = 10).generate(str(text))
plt.imshow(word_cloud)
display(fig)

【问题讨论】:

    标签: python matplotlib azure-blob-storage


    【解决方案1】:

    根据我的研究,您不能将 Matplotlib 输出直接保存到 Azure Blob 存储。

    您可以按照以下步骤将 Matplotlib 输出保存到 Azure Blob Storage:

    第 1 步:您需要先将其保存到 Databrick 文件系统 (DBFS),然后将其复制到 Azure Blob 存储。

    将 Matplotlib 输出保存到 Databricks 文件系统 (DBFS):我们使用以下命令将输出保存到 DBFS:plt.savefig('/dbfs/myfolder/Graph1.png')

    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.savefig('/dbfs/myfolder/Graph1.png')
    

    第 2 步:将文件从 Databricks 文件系统复制到 Azure Blob 存储。

    有两种方法可以将文件从 DBFS 复制到 Azure Blob Stroage。

    方法一:直接访问 Azure Blob 存储

    通过设置“Spark.conf.set”直接访问 Azure Blob 存储并将文件从 DBFS 复制到 Blob 存储。

    spark.conf.set("fs.azure.account.key.< Blob Storage Name>.blob.core.windows.net", "<Azure Blob Storage Key>")
    

    使用 dbutils.fs.cp 将文件从 DBFS 复制到 Azure Blob 存储:

    dbutils.fs.cp('dbfs:/myfolder/Graph1.png', 'wasbs://<Container>@<Storage Name>.blob.core.windows.net/Azure')
    

    方法二:将 Azure Blob 存储容器挂载到 DBFS

    您可以将 Blob 存储容器或容器内的文件夹装载到 Databricks 文件系统 (DBFS)。 mount 是一个指向 Blob 存储容器的指针,因此数据永远不会在本地同步。

    dbutils.fs.mount(
      source = "wasbs://sampledata@chepra.blob.core.windows.net/Azure",
      mount_point = "/mnt/chepra",
      extra_configs = {"fs.azure.sas.sampledata.chepra.blob.core.windows.net":dbutils.secrets.get(scope = "azurestorage", key = "azurestoragekey")})
    

    使用 dbutils.fs.cp 将文件复制到 Azure Blob 存储容器:

    dbutils.fs.cp('dbfs:/myfolder/Graph1.png', '/dbfs/mnt/chepra')

    按照 Method1 或 Method2 可以成功将输出保存到 Azure Blob Storage。

    希望这会有所帮助。如果您有任何进一步的疑问,请告诉我们。

    【讨论】:

    • 您好,如果我的回答对您有帮助,您可以接受它作为答案(单击答案旁边的复选标记,将其从灰色切换为已填充。)。这对其他社区成员可能是有益的。谢谢。
    【解决方案2】:

    我假设你已经安装了 blob 存储(如果没有,请参考databricks guide

    之后你可以关注:

    plt.figure(figsize=(20,35))
    plt.pcolor(df, cmap="gray")
    plt.yticks(np.arange(0.5, len(df.index), 1), df.index)
    plt.xticks(np.arange(0.5, len(df.columns), 1), df.columns)
    
    #create the folder where the plot needs to sit - matplotlib cannot create folders 
    # and even create an empty file with dbutils.fs.put will not work 
    dbutils.fs.mkdirs('/mnt/...base_path.../folder/')
    # save the file using /dbfs/ in front of the regular path 
    fig.savefig('/dbfs/mnt/...base_path.../folder/file_name.png') 
    

    中提琴!

    祝你好运。

    【讨论】:

      猜你喜欢
      • 2021-02-28
      • 2020-05-28
      • 2021-03-19
      • 1970-01-01
      • 2019-04-03
      • 2020-08-25
      • 2013-02-26
      • 2017-12-14
      • 2020-10-08
      相关资源
      最近更新 更多