【问题标题】:Matplotlib: boxplot stops programme executionMatplotlib:箱线图停止程序执行
【发布时间】:2019-05-11 17:56:58
【问题描述】:

我编写了一个小程序来读取 CSV 文件,并尝试从其内容中将箱线图保存到文件中:

import pandas as pd
import matplotlib.pyplot as plt

data = pd.read_csv('Data.csv')

plt.ioff()
fig1, ax1 = plt.subplots()
ax1.set_title('Test Plot')
ax1.boxplot(data)
fig1.savefig('testGraph.pdf')   # save the figure to file
plt.close(fig1, bbox_inches='tight')

Data.csv 文件的内容如下:

"VarA","VarB"
"0.17930210790378986","886.9933011822859"
"0.01364054450557276","1139.6074779392254"
"0.17926365089066673","887.0276399462234"
"0.013813899304885247","1139.5782227339264"
"0.3265506417295615","2403.651444000035"
"0.5276772326905425","550.0787718046192"
"0.3266505322109829","2403.473910859639"
"0.5270364628724318","550.3655399414718"
"0.301951691229673","2447.2094472630693"
"0.5592257235158642","536.8017995252469"
"0.3020484011262961","2447.0390369668467"
"0.5584914299935637","537.1205945626922"
"0.2778648677077672","2489.339898509246"
"0.5928427006170575","523.2229700948385"
"0.2779586849926319","2489.177223805282"
"0.5919947811607554","523.5775073154801"
"0.2542483867446956","2529.825433671251"
"0.6287276395737614","509.40461834459865"
"0.2543395881647406","2529.6710855743004"
"0.6277398978255627","509.7994161108826"

无论是通过交互式控制台,还是直接将代码提供给解释器,执行都会在ax1.boxplot(data) 行停止,不会显示任何错误消息或抛出异常。控制台中的示例:

$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> import pandas as pd
>>> import matplotlib.pyplot as plt
>>> hammer = pd.read_csv('Hammer.csv')
>>> plt.ioff()
>>> fig1, ax1 = plt.subplots()
>>> ax1.set_title('Basic Plot')
Text(0.5,1,'Basic Plot')
>>> ax1.boxplot(hammer)

之后控制台变得无响应。如果我杀死控制台,则会引发异常,但它并没有说明停止的原因:

^CTraceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/matplotlib/__init__.py", line 1718, in inner
    return func(ax, *args, **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/axes/_axes.py", line 3378, in boxplot
    manage_xticks=manage_xticks, zorder=zorder)
  File "/usr/lib/python3/dist-packages/matplotlib/axes/_axes.py", line 3730, in bxp
    boxes.extend(doplot(box_x, box_y, **final_boxprops))
  File "/usr/lib/python3/dist-packages/matplotlib/axes/_axes.py", line 3648, in doplot
    return self.plot(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/__init__.py", line 1718, in inner
    return func(ax, *args, **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/axes/_axes.py", line 1376, in plot
    self.autoscale_view(scalex=scalex, scaley=scaley)
  File "/usr/lib/python3/dist-packages/matplotlib/axes/_base.py", line 2280, in autoscale_view
    y_stickies = sum([sticky.y for sticky in stickies], [])
  File "/usr/lib/python3/dist-packages/matplotlib/axes/_base.py", line 2280, in <listcomp>
    y_stickies = sum([sticky.y for sticky in stickies], [])
KeyboardInterrupt

是什么阻碍了程序的全面执行?还是发出错误信息?

【问题讨论】:

  • @ImportanceOfBeingErnest 你是对的,CSV 中的值必须在绘制之前转换为数字。您能否提交您的评论作为答案?

标签: python pandas matplotlib boxplot


【解决方案1】:

我可以想象您还没有将字符串转换为数字,因此箱线图会尝试对字符串进行统计,并且需要很长时间才能完成。

你可以试试

ax1.boxplot(hammer.values.astype(float))

【讨论】:

    【解决方案2】:

    我对你的初始化代码做了一些小的改动,它工作了..

    import pandas as pd
    import matplotlib.pyplot as plt
    
    
    def main():
        hammer = pd.read_csv('data.csv')
    
        plt.ioff()
        fig1, ax1 = plt.subplots()
        ax1.set_title('Test Plot')
        ax1.boxplot(hammer)
        fig1.savefig('testGraph.pdf')   # save the figure to file
        plt.close(fig1)
    
    
    if __name__ == '__main__': main()
    

    我在 python 3.7 上测试它 另外,我在 windows 机器上运行它,而不是在 linux 机器上运行它

    【讨论】:

    • 这个例子也被ax1.boxplot(hammer)指令阻塞。 sn-p 中确实存在一些问题,诚然未经测试。但是交互式控制台中的示例原本是正确的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-30
    • 1970-01-01
    • 2019-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多