首页 > 解决方案 > 绘制包含字符串的数据框列

问题描述

我的数据框中有一列包含以下值之一:直/上/下/左/右,我想制作一个图,以便在每个帧中轻松识别注视方向。我尝试了什么:

plt.figure(figsize=(10, 10))
plt.plot(df_patient['gaze_direction']=='up', label='UP')
plt.plot(df_patient['gaze_direction']=='down', label='DOWN')
plt.plot(df_patient['gaze_direction']=='left', label='left')
plt.plot(df_patient['gaze_direction']=='right', label='right')
plt.plot(df_patient['gaze_direction']=='straight', label='straight')
  
# Change the number of columns here
plt.legend(ncol=5)

plt.xlabel('Frame Number')
plt.ylabel('Gaze Direction')
  
plt.show()

但我得到的图表并不清楚: 在此处输入图像描述

如何绘制此类信息以提供更多信息并易于分析?

我想制作一个“脉冲图”,这样直线将获得 0 值,例如向上/向下将获得 0.5/-0.5 ..

conditions = [
(df_patient["gaze_direction"] == 'up'),
(df_patient["gaze_direction"] == 'down'),
(df_patient["gaze_direction"] == 'left'),
(df_patient["gaze_direction"] == 'right'),
(df_patient["gaze_direction"] == 'straight')
]

# create a list of the values we want to assign for each condition
values = [0.5, -0.5, 1, -1, 0]

# create a new column and use np.select to assign values to it using our lists as arguments
df_patient['gaze_direction_plt_vals'] = np.select(conditions, values)

我绘制了它的条形图(使用 plt.plot 它非常密集): 在此处输入图像描述

有没有更好的办法?

标签: pythonmatplotlib

解决方案


推荐阅读