'Line2D' object has no property 'Label'; horizontal line in matplotlib chart will not display using axhline pycharm 3.9 to 3.10 switch

Viewed 580

I upgraded to a new PC and switched versions to 3.10 from 3.9. I cannot use the exact same code now because of an error: 'Line2D' object has no property 'Label'. I have troubleshooted for a few hours with no progress. I commented out the axhline and the legend (line 53, 54) and the code runs and plot shows, but of course my horizontal line is now gone.

'import pandas as pd
from matplotlib import pyplot as plt
from matplotlib import dates as mpl_dates
from matplotlib.lines import Line2D # for making the custom legend (line 87)
import matplotlib.ticker as ticker
from matplotlib import legend

from pylab import rcParams 
rcParams['figure.figsize'] = 8, 6

plt.style.use('fast')

@ticker.FuncFormatter
def major_formatter(x, pos):
    label = str(-x) if x < 0 else str(x)
    return label

Stage= pd.read_csv('GRL_Stage_2021.csv')
Stage['OBS DATE'] = pd.to_datetime(Stage['OBS DATE'])
Stage.sort_values('OBS DATE', inplace=True)
fig = plt.figure()

Rain= pd.read_csv('FlowData_2021_WY.csv')

x= Stage['OBS DATE']
y= Stage['BTC_HF']
y2=Stage['DES_DPI']

fig,ax= plt.subplots() #needed for creating .ax type (defined axes)
ax2=ax.twinx() # for the second Y axis

ax2.yaxis.set_major_formatter(major_formatter)# this relates to the formatter at the top

ax.plot(x,y, color= 'blue', markersize='2', linewidth= '2', label='Sacramento R. @ Butte 
City')
ax2.bar(x,y2, color='grey', alpha= .85, width=.6, label= 'Rain(in)')

y_val2= 80000 # monitor stage

#ax.axhline(y=y_val2, color = 'chartreuse', zorder=0, linestyle= '--', Label = 'Monitor 
#Stage 80k CFS')                 ******lines 53,54******
#plt.legend(fontsize='small')

ax.fill_between(x, y, color= 'darkseagreen', alpha= .75)
ax.legend(loc='center', bbox_to_anchor=(0.18, 0.82),fontsize='small')
ax2.legend(loc='center', bbox_to_anchor=(0.1, 0.83), fontsize='small') #bbox is x,y location 
of legend

ax.set_xlabel('Date')
ax.set_ylabel('River Flow (CFS)')
ax2.set_ylabel('Daily Rain Totals (In.)')
ax.set_title('Sacramento River (Butte City) Observed Daily Flow and Rain Totals')
fig.patch.set_facecolor('skyblue')
plt.gcf().autofmt_xdate()

date_format = mpl_dates.DateFormatter('%b, %d')
plt.gca().xaxis.set_major_formatter(date_format)

print(plt.style.available)
plt.tight_layout()
ax.margins(x=0) 
ax.set_ylim(ymin=0)  
print(plt.style.available)

custom_lines = [Line2D([0], [0], color='magenta', alpha=.75, lw=2, linestyle='--'),
                Line2D([0], [0], color='chartreuse', alpha=.75, lw=2, linestyle= '--'),]

ax.legend(custom_lines, ['Levee/Channel Overflow (160k CFS)', 'Flood Risk Warning! (80k 
CFS)'],facecolor='yellow',
          fontsize='small',loc='center', bbox_to_anchor=(0.42, 0.68),)'
plt.show()

Traceback (most recent call last): File "C:\Users\rgreat\PycharmProjects\pythonProject\pythonProject\BTC WxFig.py", line 53, in ax.axhline(y=y_val2, color = 'chartreuse', zorder=0, linestyle= '--', Label = 'Monitor Stage 80k CFS') File "C:\Users\rgreat\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\axes_axes.py", line 737, in axhline l = mlines.Line2D([xmin, xmax], [y, y], transform=trans, **kwargs) File "C:\Users\rgreat\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\lines.py", line 393, in init self.update(kwargs) File "C:\Users\rgreat\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\artist.py", line 1064, in update raise AttributeError(f"{type(self).name!r} object " AttributeError: 'Line2D' object has no property 'Label'

0 Answers
Related