“Line2D”对象没有属性“标签”; matplotlib 图表中的水平线将不会使用 axhline pycharm 3.9 到 3.10 开关显示
我升级到了一台新电脑,并将版本从 3.9 切换到了 3.10。由于错误,我现在无法使用完全相同的代码:“Line2D”对象没有属性“Label”。我已经解决了几个小时的问题,但没有任何进展。我注释掉了 axhline 和图例(第 53、54 行),代码运行并显示绘图,但当然我的水平线现在消失了。
'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()
回溯(最近一次调用最后一次): 文件“C:\Users\rgreat\PycharmProjects\pythonProject\pythonProject\BTC WxFig.py”,第 53 行,位于 ax.axhline(y=y_val2, color = 'chartreuse', zorder=0, linestyle= '--', Label = '监控阶段 80k CFS') 文件“C:\Users\rgreat\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\axes_axes.py”,第 737 行,位于 axhline l = mlines.Line2D([xmin, xmax], [y, y], 变换=trans, **kwargs) 文件“C:\Users\rgreat\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\lines.py”,第 393 行,在 init 中 自我更新(kwargs) 文件“C:\Users\rgreat\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\artist.py”,第 1064 行,更新中 引发 AttributeError(f"{type(self).name!r} object " AttributeError:“Line2D”对象没有属性“Label”
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'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论