1

I have loaded one column each from two different dataframes and am plotting them on a line graph. The graph pops up on my screen, but my plt.savefig command is not working, as no files are saved.

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)

1 Answer 1

3

try this:

>>> "%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

as you see you have a priority problem between the % operator and the + operator there:

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

'Subj%s_left_LEC_DGCA3.png' % betaNum is first computed to a string, then python tries to add 1 to this string, which explains the error (you didn't post any error, but the fact that it doesn't save gives that away)

I would do:

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

or even better:

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

That said, getting hold of a console where you can see exceptions that your code raises would help greatly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.