secondary_y =真正改变 pandas 的x轴 [英] secondary_y=True changes x axis in pandas

查看:101
本文介绍了secondary_y =真正改变 pandas 的x轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从不同的数据框中在熊猫中绘制两个系列.

I'm trying to plot two series together in Pandas, from different dataframes.

它们的轴都是日期时间对象,因此可以将它们绘制在一起:

Both their axis are datetime objects, so they can be plotted together:

amazon_prices.Close.plot()
data[amazon].BULL_MINUS_BEAR.resample("W").plot()
plt.plot()

收益:

很好,但是我需要绿色图表具有自己的比例.所以我用

All fine, but I need the green graph to have its own scale. So I use the

amazon_prices.Close.plot()
data[amazon].BULL_MINUS_BEAR.resample("W").plot(secondary_y=True)
plt.plot()

此secondary_y产生了一个问题,因为我没有以下图形:

This secondary_y creates a problem, as instead of having the desired graph, I have the following:

在此方面提供的任何帮助均深表感谢.

Any help with this is hugely appreciated.

(较少相关注释:我(显然)正在使用Pandas,Matplotlib,而所有这些都在Ipython笔记本中)

(Less relevant notes: I'm (evidently) using Pandas, Matplotlib, and all this is in an Ipython notebook)

从那以后,我注意到删除resample("W")可以解决此问题.但是,这仍然是一个问题,因为未重采样的数据太嘈杂而看不见.能够绘制具有辅助轴的采样数据将非常有帮助.

I've since noticed that removing the resample("W") solves the issue. It is still a problem however as the non-resampled data is too noisy to be visible. Being able to plot sampled data with a secondary axis would be hugely helpful.

推荐答案

import matplotlib.pyplot as plt
import pandas as pd
from numpy.random import random

df = pd.DataFrame(random((15,2)),columns=['a','b'])
df.a = df.a*100

fig, ax1 = plt.subplots(1,1)
df.a.plot(ax=ax1, color='blue', label='a')
ax2 = ax1.twinx()
df.b.plot(ax=ax2, color='green', label='b')
ax1.set_ylabel('a')
ax2.set_ylabel('b')
ax1.legend(loc=3)
ax2.legend(loc=0)
plt.show()

这篇关于secondary_y =真正改变 pandas 的x轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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