python - 绘制包含字符串的数据框列
问题描述
我的数据框中有一列包含以下值之一:直/上/下/左/右,我想制作一个图,以便在每个帧中轻松识别注视方向。我尝试了什么:
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)
有没有更好的办法?
解决方案
推荐阅读
- r - 在 R Notebook 中,如何设置我的 API 密钥而不直接将其保存在 Notebook 中?
- php - Textarea 不通过 Post
- swift - 如何为 0 行的消息输入创建 TextField?
- c# - 动态创建 T 时的 typeof(T) 为空?
- java - JsonIgnore 不适用于 Spring Security 用户和角色
- php - simplexml_load_file 不会使用数组获取数据
- r - ROracle 已安装,但安装依赖包时加载失败
- javascript - 仅从数据层检索 SKU
- docker - 码头工人无法找到文件
- mysql - mysql 今天分组