PANDAS绘制多个Y轴 [英] PANDAS plot multiple Y axes

查看:263
本文介绍了PANDAS绘制多个Y轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道pandas支持次要Y轴,但我很好奇是否有人知道在图上放置三次Y轴的方法...目前,我正在使用numpy + pyplot实现此功能...但是在处理大数据时速度较慢设置.

I know pandas supports a secondary Y axis, but Im curious if anyone knows a way to put a tertiary Y axis on plots... currently I am achieving this with numpy+pyplot ... but it is slow with large data sets.

这是为了在同一张图上绘制具有不同单位的不同测量值,以便于比较(例如,相对湿度/温度/和电导率)

this is to plot different measurements with distinct units on the same graph for easy comparison (eg Relative Humidity/Temperature/ and Electrical Conductivity)

所以真的很好奇,是否有人知道在没有太多工作的情况下在pandas中是否可能.

so really just curious if anyone knows if this is possible in pandas without too much work.

我怀疑是否有一种方法可以做到这一点(没有太多的开销),但是我希望被证明是错误的,这可能是matplotlib的局限性...

I doubt that there is a way to do this(without too much overhead) however I hope to be proven wrong , this may be a limitation of matplotlib...

推荐答案

我认为这可能有效:

import matplotlib.pyplot as plt
import numpy as np
from pandas import DataFrame
df = DataFrame(np.random.randn(5, 3), columns=['A', 'B', 'C'])

fig, ax = plt.subplots()
ax3 = ax.twinx()
rspine = ax3.spines['right']
rspine.set_position(('axes', 1.15))
ax3.set_frame_on(True)
ax3.patch.set_visible(False)
fig.subplots_adjust(right=0.7)

df.A.plot(ax=ax, style='b-')
# same ax as above since it's automatically added on the right
df.B.plot(ax=ax, style='r-', secondary_y=True)
df.C.plot(ax=ax3, style='g-')

# add legend --> take advantage of pandas providing us access
# to the line associated with the right part of the axis
ax3.legend([ax.get_lines()[0], ax.right_ax.get_lines()[0], ax3.get_lines()[0]],\
           ['A','B','C'], bbox_to_anchor=(1.5, 0.5))

输出:

这篇关于PANDAS绘制多个Y轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆