2017-08-30 128 views
0

我已经从两个不同的数据框中加载了一列,并将它们绘制在线图上。图表弹出在我的屏幕上,但我的plt.savefig命令不起作用,因为没有文件被保存。matplotlib savefig不输出文件

import matplotlib.pyplot as plt 
import plotly.plotly as py 
import pandas as pd 
import plotly.graph_objs as go 

# read in LEC 
LLEC = pd.read_csv('LLEC_1.csv').transpose() 
RLEC = pd.read_csv('RLEC_2.csv').transpose() 

#read in DGCA3 
LDGCA3=pd.read_csv('LDGCA3_13.csv').transpose() 
RDGCA3 = pd.read_csv('RDGCA3_14.csv').transpose() 

def plot_betas_left(betaNum): 
    betaNum = int(betaNum) 

    #plot betas for both ROIs. start with LEC 
    ax = LLEC[betaNum].plot() 
    # add DGCA3 
    LDGCA3[betaNum].plot(ax=ax) 
    # label axes 
    ax.set_xlabel("precise beta number (0 is first)") 
    ax.set_ylabel("beta coefficient value") 
    # inset legend 
    ax.legend(['LEC', 'DGCA3']) 
    plt.savefig('Subj%s_left_LEC_DGCA3.png' % betaNum+1) 

plot_betas(3) 

回答

3

试试这个:

>>> "%s" % 12+1 
Traceback (most recent call last): 
    File "<string>", line 301, in runcode 
    File "<interactive input>", line 1, in <module> 
TypeError: Can't convert 'int' object to str implicitly 

,你看你有没有%运营商和+运营商有之间的优先级问题:

plt.savefig('Subj%s_left_LEC_DGCA3.png' % betaNum+1) 

'Subj%s_left_LEC_DGCA3.png' % betaNum首先计算为一个字符串,那么python会尝试将1添加到该字符串中,这就解释了错误(您没有发布任何错误,但事实上t将其不保存给出的距离)

我会做:

plt.savefig('Subj%s_left_LEC_DGCA3.png' % (betaNum+1)) 

甚至更​​好:

plt.savefig('Subj{}_left_LEC_DGCA3.png'.format(betaNum+1)) 

这就是说,得到一个控制台保持在那里你可以看到例外你的代码会提高很大。

相关问题