我的数据源已更改,更新从分组数据创建绘图的代码时遇到问题。我能够成功地绘制数据的一个子集,但是我需要添加一个条件,在同一个图表上绘制两个子集。有没有一种方法可以让我的for/if/elif实现这一点
但是,处理过的\u-eye值同时包含OD和OS,我需要在同一个图表上绘制它们:当OD时,我需要绘制*\u-OD列值,当OS时,我需要绘制*\u-OS列值,因此我修改了代码并得到一个值错误:
# plot for new data
for subject_group, sub_df in new_df.groupby(by='subject_group'):
if new_df.treated_eye == 'OD':
plt.plot(sub_df['visit_number'], sub_df['white_od'], marker='o', label=subject_group)
elif new_df.treated_eye == 'OS':
plt.plot(sub_df['visit_number'], sub_df['white_os'], marker='o', label=subject_group)
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
# add a normal line at -65
# plt.ylim(ymin=-70, ymax=-30) # add ymax to make both color plots consistent
plt.xticks(range(0, new_od_df['visit_number'].max() + 1 , 1))
plt.xlabel('Visit (Months)')
plt.ylabel('Threshold (dB)')
# add a dashed green line and "normal"
plt.axhline(-65, color="green", linestyle='--', dashes=(5, 10)) #length of 5, space of 10
plt.text(13, -65, 'Normal', va='center', ha="left", bbox=dict(facecolor="w",alpha=0.5))
plt.title('RPGR-001: White FST (Treated Eye)')
plt.tight_layout()
# plt.savefig('output/rpgr_fst_white_teye.png')
ValueError Traceback (most recent call last) in () 1 # plot for new data 2 for subject_group, sub_df in new_df.groupby(by='subject_group'): ----> 3 if new_df.treated_eye == 'OD': 4 plt.plot(sub_df['visit_number'], sub_df['white_od'], marker='o', label=subject_group) 5 elif new_df.treated_eye == 'OS':
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\generic.py in nonzero(self) 1574 raise ValueError("The truth value of a {0} is ambiguous. " 1575 "Use a.empty, a.bool(), a.item(), a.any() or a.all()." -> 1576 .format(self.class.name)) 1577 1578 bool = nonzero
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
问题不在于
matplotlib
,而在于if
语句new_df.treated_eye
和new_df.treated_eye
是数据帧中包含多个条目的列。当您执行if new_df.treated_eye == 'OD'
时,当new_df.treated_eye
是一个列表时,您将其视为一个变量。由于要在所有元素满足此条件时进行绘图,因此需要使用all()
因此,请尝试以下方法
相关问题 更多 >
编程相关推荐