Matplotlib:boxplot停止程序执行

时间:2018-12-10 19:28:09

标签: python pandas matplotlib boxplot

我编写了一个小程序来读取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

是什么导致程序无法完全执行?还是发出错误消息?

2 个答案:

答案 0 :(得分:1)

我可以想象您还没有将字符串转换为数字,这样boxplots会尝试对字符串进行统计,并且要花很长时间才能完成。

您可以尝试

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

答案 1 :(得分:0)

我对您的初始化代码进行了较小的更改,然后它起作用了。

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机器上运行它