pythonmatplotlibanacondapython-ggplotjupyter-lab

Trouble with matplotlib after installing ggplot


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'

Solution

  • 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.