【发布时间】:2015-06-10 09:36:03
【问题描述】:
我在这里迷路了,可能混淆了 matplotlib 和 pandas 功能。我有一个数据框,我想在几个子图中绘制以对相关数据进行分组。一切都按预期工作,但我无法让图例工作(如果我不使用子图,这非常简单)。这是我的代码:
fig, (acceleration, rotation, rotationspeed) = plt.subplots(3, 1, sharex=True, figsize=(20,15))
acceleration.plot(params[['accX', 'accY', 'accZ']], label=['X', 'Y', 'Z'])
acceleration.set_title('Acceleration')
acceleration.set_ylabel('Acceleration (G)')
acceleration.grid(b = pref_grid_visible)
acceleration.legend()
rotation.plot(params[['roll', 'pitch', 'yaw']])
rotation.set_title('Rotation')
rotation.set_ylabel('Rotation angle (radians)')
rotation.grid(b = pref_grid_visible)
rotationspeed.plot(params[['rotX', 'rotY', 'rotZ']])
rotationspeed.set_title('Rotation speed')
rotationspeed.set_ylabel('Rotation speed (radians / second)')
rotationspeed.grid(b = pref_grid_visible)
# Set shared x label
fig.text(0.5, 0.1, 'Time (ms)')
标签不会自动从数据框中读取,从 matplotlib 的文档中我了解到label 应该包含一个字符串,这解释了为什么显示 X、Y 和 Z 轴以进行加速的尝试不起作用。
如何为每个子图正确设置 X、Y 和 Z 标签?
【问题讨论】:
-
您的代码中的
params是什么?请把它设为MWVE 好吗? -
抱歉,
params是数据帧的(子集)。
标签: python pandas matplotlib