如何在2个图的水平线中显示年份数据?

发布于 2025-01-30 04:21:50 字数 1317 浏览 0 评论 0原文

我有一个关于情节的问题。我有一个名为 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 :

enter image description here

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 :

enter image description here

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)

And this is the result :
enter image description here

How can I remove 3 title 'Date' in x-axis of each plot ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扛刀软妹 2025-02-06 04:21:50

由于所有三个图的时间索引都是相同的,因此您可以在所有三个plot呼叫中使用sharex = false(默认值),并且您应该获取所有XTICKS并且仍然正确对齐的绘图:

log_return_data['HPQ_Returns'][1377:].plot(ax=axis[0], title='HPQ', fontsize=8, sharex=False, linewidth=0.6)
log_return_data['MSFT_Returns'][1377:].plot(ax=axis[1], title='MSFT', fontsize=8,sharex=False, linewidth=0.6)
log_return_data['INTC_Returns'][1377:].plot(ax=axis[2], title='INTC', fontsize=8,sharex=False, linewidth=0.6)

sharex = true将在您拥有不同的串行序列的不同时间索引索引并非完全对齐时会产生不同的绘图对齐,但这不是您的情况。

评论后编辑:

从现在开始,标题与轴标签和轴勾号标签重叠,您可以在plt.savefig()::

# This removes the 'Date' label from every axis except the last.
for ax in axis[:-1]:
    ax.set_xlabel('')

# This rearranges the layout (the title still would overlap with the tick labels).
plt.tight_layout()

Since the time index is the same for all three plot, you can use sharex=False in all three plot calls (which is the default) and you should get all the xticks and still have the plots aligned correctly:

log_return_data['HPQ_Returns'][1377:].plot(ax=axis[0], title='HPQ', fontsize=8, sharex=False, linewidth=0.6)
log_return_data['MSFT_Returns'][1377:].plot(ax=axis[1], title='MSFT', fontsize=8,sharex=False, linewidth=0.6)
log_return_data['INTC_Returns'][1377:].plot(ax=axis[2], title='INTC', fontsize=8,sharex=False, linewidth=0.6)

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():

# This removes the 'Date' label from every axis except the last.
for ax in axis[:-1]:
    ax.set_xlabel('')

# This rearranges the layout (the title still would overlap with the tick labels).
plt.tight_layout()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文