图形添加误差线

在使用matplotlib绘制图形时,如果想在图形上添加误差线,但现有的代码却一直报错。以下是报错前的代码,其中以注释形式添加了希望添加的误差线代码。数据文件包含四列数据,其中第四列为纵向误差。当运行包含注释行的新代码时,会出现如下错误:
在这里插入图片描述

Traceback (most recent call last):
File "39.py", line 37, in <module>
plot_graph()
File "39.py", line 29, in plot_graph
plt.errorbar(x1,y1, yerr = z1, marker = 'none', linestyle = 'none')
File "/Users/Bashe/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/pyplot.py", line 2697, in errorbar
errorevery=errorevery, capthick=capthick, **kwargs)
File "/Users/Bashe/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/axes.py", line 5758, in errorbar
in cbook.safezip(y, yerr)]
TypeError: unsupported operand type(s) for -: 'str' and 'str'

以下是完整的代码示例:

import os
import pylab as plt

def plot_graph():
    file='Graph.txt'
    x = []
    y = []
    #z = []
    x1 = []
    y1 = []
    #z1 = []
    t = []
    t1 = []
    for dirpath,dirs,files in os.walk('/Users/Bashe/Desktop/121210 p2/'):
        if file in files:
            infile = open(os.path.join(dirpath, "Graph.txt"), "r")
            for columns in (raw.strip().split() for raw in infile):
                t = columns[0]
                x = columns[1]          
                y = columns[2]
                #z = columns[3]
                x1.append(str(x))
                y1.append(str(y))
                #z1.append(str(z))
            t1.append(str(t))
            savepath = os.path.join(dirpath, 'Mean vs Temperature for %s.png' %(t1[0]))
            plt.plot(x1,y1, marker ='o', linestyle = '--')
            #plt.errorbar(x1,y1, yerr = z1, marker = 'none', linestyle = 'none')
            plt.xlabel('Temperature')
            plt.ylabel('Mean')
            plt.title('Mean vs Temperature for %s probe concentration' %(t1[0]))
            plt.savefig(savepath)
            #plt.show()
            infile.close()


2、解决方案

根据错误提示,可以发现问题在于将x1和y1作为字符串添加到列表中,然后再尝试绘制它们。为了解决此问题,需要将它们作为浮点数传递,因为原始的x和y应该是浮点数。

下面是修改后的代码:

import os
import pylab as plt

def plot_graph():
    file='Graph.txt'
    x = []
    y = []
    z = []
    x1 = []
    y1 = []
    z1 = []
    t = []
    t1 = []
    for dirpath,dirs,files in os.walk('/Users/Bashe/Desktop/121210 p2/'):
        if file in files:
            infile = open(os.path.join(dirpath, "Graph.txt"), "r")
            for columns in (raw.strip().split() for raw in infile):
                t, x, y, z = [float(v) for v in columns]
                x1.append(x)
                y1.append(y)
                z1.append(z)
            t1.append(str(t))
            savepath = os.path.join(dirpath, 'Mean vs Temperature for %s.png' %(t1[0]))
            plt.plot(x1,y1, marker ='o', linestyle = '--')
            plt.errorbar(x1,y1, yerr = z1, marker = 'none', linestyle = 'none')
            plt.xlabel('Temperature')
            plt.ylabel('Mean')
            plt.title('Mean vs Temperature for %s probe concentration' %(t1[0]))
            plt.savefig(savepath)
            plt.show()
            infile.close()

plot_graph()

上述代码中,使用列表推导将t、x、y和z转换为浮点数,然后将它们添加到相应的列表中。还添加了plt.show()来显示最终的图

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值