【问题标题】:Using `matplotlib` to plot - <Figure size 432x288 with 0 Axes>使用 `matplotlib` 绘图 - <Figure size 432x288 with 0 Axes>
【发布时间】:2022-05-11 16:48:47
【问题描述】:

我有一个包含 158 列和 310 万行的庞大数据集。我正在尝试为该数据集绘制单变量分布。代码如下。

dtf = pd.read_csv('hackathon_train_data1.csv')
dtf.head()
dtf.columns

输出是:

索引(['visit_id', 'cod_order_nbr', 'cod_orig_ord_nbr', 'src_bu_id', 'int_ref_nbr'、'cod_orig_bu_id'、'cod_src_bu_id'、'onln_flg'、 'sohf_ord_dt', 'cod_init',
...
'csat_guid_v42'、'visit_num'、'chat_drawer_rightrail_open'、 'chat_unavailable'、'chat_portal'、'ishmximpressions'、'pagination_c40'、 'chat_intent_flag'、'coupon_code_stp_v96'、'isbreadcrumbhit_flg']、 dtype='object', 长度=157)

然后我将其中一个列名分配给y 并绘制了图表。 cod_flg 列只有 2 个条目,01

y = "cod_flg"  
ax = dtf[y].value_counts().sort_values().plot(kind="barh")  

输出为:

然后我尝试将其细化为,

totals= []
for i in ax.patches:
    totals.append(i.get_width())
total = sum(totals)
for i in ax.patches:
     ax.text(i.get_width()+.3, i.get_y()+.20, 
     str(round((i.get_width()/total)*100, 2))+'%', 
     fontsize=10, color='black')
ax.grid(axis="x")
plt.suptitle(y, fontsize=20)
plt.show()  

它给了我这个错误:

图形大小 432x288,0 轴

我需要修改这一行吗? ax.text(i.get_width()+.3, i.get_y()+.20, str(round((i.get_width()/total)*100, 2))+'%', fontsize=10, color='black')

【问题讨论】:

  • 在那个链接中,它说我们应该首先设置figsize,我没有在我的代码中使用figsize
  • 你确定这是一个错误,而不是 Jupyter 只是写出最后一个命令的结果吗?您是否尝试将所有命令放入同一个单元格?你能显示完整的错误跟踪吗?
  • 实际上不是错误,jupyter 正在打印它。但这没有任何意义,除非我的代码中有一些错误,需要帮助才能弄清楚

标签: python matplotlib


【解决方案1】:

如果您使用的是 Google Colab,请尝试不使用 plt.show()

【讨论】:

    猜你喜欢
    • 2020-01-21
    • 2019-03-20
    • 2021-01-28
    • 1970-01-01
    • 1970-01-01
    • 2016-07-02
    • 1970-01-01
    • 1970-01-01
    • 2010-11-27
    相关资源
    最近更新 更多