如何在2个图的水平线中显示年份数据?
我有一个关于情节的问题。我有一个名为 log_return_data 的数据框。 这是数据框的快照:
这是我写的代码:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
fig1, axis = plt.subplots(3,1,figsize=(9,6),dpi=100)
log_return_data['HPQ_Returns'][1377:].plot(ax=axis[0], title='HPQ', fontsize=8, sharex=True, linewidth=0.6)
log_return_data['MSFT_Returns'][1377:].plot(ax=axis[1], title='MSFT', fontsize=8,sharex=True, linewidth=0.6)
log_return_data['INTC_Returns'][1377:].plot(ax=axis[2], title='INTC', fontsize=8,sharex=True, linewidth=0.6)
plt.savefig('returns')
这就是结果:
但是,我希望这一年的数据不仅在最后一个图中(INTC数据),而且在HPQ和MSFT中(2个首先图)。 反正解决这个问题吗? 谢谢。
##编辑 : 在第一个答案器的帮助下,我将代码更改为:
log_return_data['HPQ_Returns'][1377:].plot(ax=axis[0], title='HPQ', fontsize=8, linewidth=0.6)
如何在每个图的x轴中删除3个标题'日期'?
I have a question regarding the plots. I have a dataframe named log_return_data.
Here is the snapshot of the dataframe :
I want to plot 3 plots of each returns in 1 plots and the corresponding time ( in column Index).
Here is the code I wrote:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
fig1, axis = plt.subplots(3,1,figsize=(9,6),dpi=100)
log_return_data['HPQ_Returns'][1377:].plot(ax=axis[0], title='HPQ', fontsize=8, sharex=True, linewidth=0.6)
log_return_data['MSFT_Returns'][1377:].plot(ax=axis[1], title='MSFT', fontsize=8,sharex=True, linewidth=0.6)
log_return_data['INTC_Returns'][1377:].plot(ax=axis[2], title='INTC', fontsize=8,sharex=True, linewidth=0.6)
plt.savefig('returns')
And this is the result :
However, I want the Year Data is not only in the last plot ( of INTC data) but also in the HPQ and MSFT ( 2 first plots).
Is there anyway solving this problem ?
Thanks.
##Edit :
With the help of the 1st answerer, I change to code to :
log_return_data['HPQ_Returns'][1377:].plot(ax=axis[0], title='HPQ', fontsize=8, linewidth=0.6)
How can I remove 3 title 'Date' in x-axis of each plot ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于所有三个图的时间索引都是相同的,因此您可以在所有三个
plot
呼叫中使用sharex = false
(默认值),并且您应该获取所有XTICKS并且仍然正确对齐的绘图:sharex = true
将在您拥有不同的串行序列的不同时间索引索引并非完全对齐时会产生不同的绘图对齐,但这不是您的情况。评论后编辑:
从现在开始,标题与轴标签和轴勾号标签重叠,您可以在
plt.savefig()
::Since the time index is the same for all three plot, you can use
sharex=False
in all threeplot
calls (which is the default) and you should get all the xticks and still have the plots aligned correctly:sharex=True
would generate a different alignment of the plots when you have different series with different time indices not perfectly aligning, but it's not your case.Edit after comments:
Since now the titles overlap with both the axis label and the axis tick labels, you can try the following before
plt.savefig()
: