0

Yesterday, I installed the ggplot to my anaconda environment. When I tried to use the matplotlib plot that worked before I installed ggplot I am getting the below error. I am also getting errors from other inline jupyter lab codes. Any help will be appreciated. I am new to visulizing data. If there is a another plotting module I should use let me know.

plt.rcParams['figure.dpi'] = 200
plt.rcParams.update({'font.size': 5})

fig, ax1 = plt.subplots()
ax1.set_xlabel('Time')

ax1.set_ylabel('price', color='k')
ax1.plot(df['price'], color='#0072b5', label = 'price')
ax1.tick_params(axis='y', labelcolor='k')
#ax1.tick_params(axis='x',  labelrotation = 90) 

ax2 = ax1.twinx()  # instantiate a second axes that shares the same x-axis#

color = 'tab:cyan'
ax2.set_ylabel('gen', color='k')  # we already handled the x-label with ax1
ax2.plot(df['gen'], color='#e2e3e2', label = 'gen')
ax2.tick_params(axis='y', labelcolor='k')

#ax1.legend(loc=2)
#ax2.legend(loc=1)
fig.legend(loc=1, bbox_to_anchor=(1,1), bbox_transform=ax1.transAxes, prop={'size':5})


fig.tight_layout()  # otherwise the right y-label is slightly clipped
fig.suptitle('%s, %s %s' % (df, month_graph, year_graph) , fontsize=8)
fig.subplots_adjust(top=0.90)
plt.savefig("%s.png" % ('genPrice'))
plt.show()

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-14-032d973b53a3> in <module>()
     19 #ax1.legend(loc=2)
     20 #ax2.legend(loc=1)
---> 21 fig.legend(loc=1, bbox_to_anchor=(1,1), bbox_transform=ax1.transAxes, prop={'size':5})
     22 
     23 

TypeError: legend() missing 2 required positional arguments: 'handles' and 'labels'
3
  • 1
    During the install of ggplot you may have changed your matplotlib version. It seems ggplot pretty much conflicts with a lot of other libraries. I am for example completely unable to get it running with the newest matplotlib 2.2.3. Commented Sep 12, 2018 at 19:34
  • It seems development of ggplot has ceased in 2016. There might be an alternative, plotnine, but I haven't tested it. Commented Sep 12, 2018 at 20:02
  • Thank you for your comment. I did upgrade my matplotlib to the newest version and so now my plot works! I will look at plotnine. Part of the appeal of ggplot was the hope I could collaborate with my colleague who uses R. Commented Sep 13, 2018 at 11:34

2 Answers 2

0

The traceback states that two "required" arguments are missing although, accordingly to the documentation, they are in fact optional. If you are experiencing this issue since installing a new module, then maybe you have downgraded matplotlib to a previous version where the two arguments were obligatory. If this is the case, you may want to pip install matplotlib --upgrade from the console.

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

3 Comments

Thank you for your comment. I appreciate the link to the code. How did you know where to find it? Are the comments listed in the code, typical for python modules? Your response was insightful. I have never seen something like this before. Thank you.
@getaglow There is a way for you to access the docstring of any function function_name, if a docstring is present at all, from Python: call help(function_name) or, if funcion_name is a method of a class ClassName, call help(ClassName.function_name). In this case, you would call help(matplotlib.figure.Figure) after import matplotlib (not matplotlib.pyplot). If you like the answer, you can select it :)
Thank you. Your responses have helped me understand better what I am doing while coding, which will make me better.
0

The signature for matplotlib.figure.Figure.legend is in version 2.0.2 of matplotlib

legend(handles, labels, *args, **kwargs)

while in version 2.1.2 or above it is

legend(*args, **kwargs)

This means you have downgraded your matplotlib during the install of ggplot. If you want to continue working with this older matplotlib version you would need to provide the handles and labels yourself. This could look like

h1, l1 = ax1.get_legend_handles_labels()
h2, l2 = ax2.get_legend_handles_labels()

fig.legend(h1+h2, l1+l2, loc=1, bbox_to_anchor=(1,1), 
           bbox_transform=ax1.transAxes, prop={'size':5})

1 Comment

Thank you. I upgraded my matplotlib and it seems to have fixed the problem. I have not tried using ggplot. When I have time later today I may try to play around with your suggestion. Thank you.

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.