Python Plot看到Reals但Bar看到了Strings

时间:2017-03-24 18:48:01

标签: python matplotlib bar-chart

简而言之,当我使用plot命令时,我的数据被识别为真正的双精度数字,但由于某种原因要求条形图会返回数据是字符串而不是实数的抱怨。这是我的简单代码:

   #!/usr/bin/env python
   import numpy as np
   import matplotlib.pyplot as plt
   #for counter in range(102,401):
   for counter in range(400,401):
   temp1=str(counter)
   temp="column_1_file_"+temp1
   temp2="column_2_file_"+temp1
   xdata=np.loadtxt(temp,delimiter=" ",dtype=str)
   ydata=np.loadtxt(temp2,delimiter=" ",dtype=str)
   plt.plot(xdata,ydata,'or')
   # width=1.0
   # plt.bar(xdata,ydata,width,color="blue")
   plt.xlabel(r' Particle Radius ($\mu$m)')
   plt.ylabel("Frequency")
   plt.title("Histogram of Particle Radius")
   plt.xlim(-1,40)
   plt.ylim(0,1.1)
   # plt.show()
   var1=str(counter)+".png"
   plt.savefig(var1,format='png')
   counter=counter+1

执行时,上面的程序工作正常,产生所需的线条图就好了。当我评论" plt.plot(xdata,ydata,'或')"并取消注释plt.bar和width行,我得到以下错误:

在命令行中,给出的命令和响应是:

    python Histogram_Evolution.py 
    Traceback (most recent call last):
     File "Histogram_Evolution.py", line 13, in <module>
       plt.bar(xdata,ydata,width,color="blue")
     File "/N/soft/rhel6/python/2.7.3/lib/python2.7/site-packages/matplotlib-                  1.3.1-py2.7-linux-x86_64.egg/matplotlib/pyplot.py", line 2515, in bar
     ret = ax.bar(left, height, width=width, bottom=bottom, **kwargs)
    File "/N/soft/rhel6/python/2.7.3/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/axes.py", line 5053, in bar
    self.add_patch(r)
    File "/N/soft/rhel6/python/2.7.3/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/axes.py", line 1562, in add_patch
    self._update_patch_limits(p)
     File "/N/soft/rhel6/python/2.7.3/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/axes.py", line 1580, in _update_patch_limits
     xys = patch.get_patch_transform().transform(vertices)
    File "/N/soft/rhel6/python/2.7.3/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/patches.py", line 576, in get_patch_transform
    self._update_patch_transform()
    File "/N/soft/rhel6/python/2.7.3/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/patches.py", line 569, in _update_patch_transform
    bbox = transforms.Bbox.from_bounds(x, y, width, height)
    File "/N/soft/rhel6/python/2.7.3/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/transforms.py", line 821, in from_bounds
    return Bbox.from_extents(x0, y0, x0 + width, y0 + height)
TypeError: cannot concatenate 'str' and 'float' objects

以下是读取的xdata文件示例,后面是读取的ydata文件示例:

0.3454E-03
0.3801E-03
0.4095E-03
0.4245E-03
0.4456E-03
0.4661E-03
0.4861E-03
0.5049E-03
0.5292E-03
0.5435E-03
0.5588E-03
0.5732E-03
0.5840E-03
0.5925E-03
0.6037E-03
0.6158E-03
0.6852E-03
0.9102E-03
0.1083E-02
0.1197E-02
0.1306E-02
0.1477E-02
0.1986E-02
0.2352E-02
0.2577E-02
0.2810E-02
0.3253E-02
0.4226E-02
0.5017E-02
0.5535E-02
0.6018E-02
0.6973E-02
0.9046E-02
0.1079E-01
0.1192E-01
0.1294E-01
0.1502E-01
0.1948E-01
0.2325E-01
0.2568E-01
0.2789E-01
0.3235E-01
0.4198E-01
0.5012E-01
0.5532E-01
0.6012E-01

0.781791E-01
0.782330E-01
0.105108E-02
0.108144E-02
0.180879E-04
0.544883E-03
0.545426E-03
0.544242E-04
0.298458E-06
0.287814E-06
0.329474E-06
0.473319E-07
0.829702E-07
0.123443E-08
0.411156E-07
0.421080E-07
0.218848E+00
0.279559E+00
0.234184E+00
0.291552E+00
0.240008E+00
0.449513E+00
0.485745E+00
0.434142E+00
0.473585E+00
0.457689E+00
0.576471E+00
0.551371E+00
0.493782E+00
0.456229E+00
0.409499E+00
0.497298E+00
0.529838E+00
0.529549E+00
0.533233E+00
0.531061E+00
0.635727E+00
0.654071E+00
0.653801E+00
0.655098E+00
0.653899E+00
0.712113E+00
0.721604E+00
0.721187E+00
0.721345E+00
0.726954E+00
0.793069E+00
0.806902E+00
0.805833E+00

1 个答案:

答案 0 :(得分:0)

Matplotlib的plot集成了一个便捷功能,可以自动将字符串转换为数字。 bar没有此功能。因此,为了绘制条形图,您需要使用数值数据。

这不应该是一个问题,只是在读取数据时不要将str指定为dtype

np.loadtxt(temp,delimiter=" ")