【问题标题】:Save pandas table (filled with strings) as png将 pandas 表(用字符串填充)另存为 png
【发布时间】:2017-11-28 18:45:37
【问题描述】:

这是我的代码:

column1 = ['Measured Set', '1. set', '2. set', '3. set']
column2= ['Breached parameter (number of breaches, %)' ]
column3 = ['Breached parameter (number of breaches, %)']

for j in range(NOT):
    column2.append(report_str[0][j])
    column3.append(report_str[1][j])

data = {
    'Sensor': column1,
    'Sensor 1': column2,
    'Sensor 2': column3,
}

df = pd.DataFrame(data)
df

report_str 是一个列表,其中填充了我将某些字符串复制到表中的字符串。

我尝试使用以下代码保存表格:

ax = df.plot()
fig = ax.get_figure()
fig.savefig('asdf.png')

但我得到一个错误:“空的'DataFrame':没有要绘制的数字数据”。

这是我要保存的输出表:

谁能帮我解决这个问题?

【问题讨论】:

    标签: python python-2.7 python-3.x pandas matplotlib


    【解决方案1】:

    首先,您的列列表必须具有相同的长度。您可以使用来自 pandas 的 matplotlib 和 table 函数绘制表格。

    import pandas as pd
    import matplotlib.pylab as plt
    from pandas.tools.plotting import table
    
    # I add None value to align all lists
    column1 = ['Measured Set', '1. set', '2. set', '3. set']
    column2= ['Breached parameter (number of breaches, %)', None, None,None ]
    column3 = ['Breached parameter (number of breaches, %)', None, None,None]
    
    data = {
        'Sensor': column1,
        'Sensor 1': column2,
        'Sensor 2': column3,
    }
    
    df = pd.DataFrame(data)
    print(df)
    
    # set fig size
    fig, ax = plt.subplots(figsize=(12, 3)) 
    # no axes
    ax.xaxis.set_visible(False)  
    ax.yaxis.set_visible(False)  
    # no frame
    ax.set_frame_on(False)  
    # plot table
    tab = table(ax, df, loc='upper right')  
    # set font manually
    tab.auto_set_font_size(False)
    tab.set_fontsize(8) 
    # save the result
    plt.savefig('table.png')
    

    【讨论】:

    • 谢谢,这段代码确实有效,但是即使文本较大,表格仍然很小,有没有办法增加行和列的大小?
    【解决方案2】:

    如果您遇到下一个异常: “没有名为 pandas.tools 的模块”

    使用以下导入: 从 pandas.plotting 导入表

    【讨论】:

      【解决方案3】:

      https://pypi.org/project/df2img/ 上有一个名为 df2img 的 Python 库(免责声明:我是作者)。这是一个使用plotly 作为后端的包装器/便利函数。

      您可以在readthedocs找到文档

      import pandas as pd
      
      import df2img
      
      df = pd.DataFrame(
          data=dict(
              float_col=[1.4, float("NaN"), 250, 24.65],
              str_col=("string1", "string2", float("NaN"), "string4"),
          ),
          index=["row1", "row2", "row3", "row4"],
      )
      

      pd.DataFrame 保存为 .png 文件可以相当快地完成。您可以应用格式,例如背景颜色或交替行颜色以提高可读性。

      fig = df2img.plot_dataframe(
          df,
          title=dict(
              font_color="darkred",
              font_family="Times New Roman",
              font_size=16,
              text="This is a title",
          ),
          tbl_header=dict(
              align="right",
              fill_color="blue",
              font_color="white",
              font_size=10,
              line_color="darkslategray",
          ),
          tbl_cells=dict(
              align="right",
              line_color="darkslategray",
          ),
          row_fill_color=("#ffffff", "#d7d8d6"),
          fig_size=(300, 160),
      )
      
      df2img.save_dataframe(fig=fig, filename="plot.png")
      

      【讨论】:

        猜你喜欢
        • 2020-11-20
        • 2020-05-31
        • 1970-01-01
        • 1970-01-01
        • 2014-07-03
        • 1970-01-01
        • 1970-01-01
        • 2017-04-11
        • 1970-01-01
        相关资源
        最近更新 更多