Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的共享x轴_Python_Pandas - Fatal编程技术网

Python中的共享x轴

Python中的共享x轴,python,pandas,Python,Pandas,通常我总是在这里得到问题的答案,所以这里有一个新的答案。我正在做一些数据分析,在那里我导入不同的csv文件,设置索引,然后我尝试绘制它 这是代码。请注意,我使用的是obdobje和-obdobje,因为索引来自不同的文件,但格式相同: #to start plotting fig, axes = plt.subplots(nrows=2, ncols=1) #first dataframe df1_D1[obdobje:].plot(ax=axes[0], linewidth=2, color

通常我总是在这里得到问题的答案,所以这里有一个新的答案。我正在做一些数据分析,在那里我导入不同的csv文件,设置索引,然后我尝试绘制它

这是代码。请注意,我使用的是
obdobje
-obdobje
,因为索引来自不同的文件,但格式相同:

#to start plotting
fig, axes = plt.subplots(nrows=2, ncols=1)

#first dataframe
df1_D1[obdobje:].plot(ax=axes[0], linewidth=2, color='b', linestyle='solid')

#second dataframe
df2_D1[obdobje:].plot(ax=axes[0], linewidth=2, color='b',linestyle='dashed')

#third data frame
df_index[:-obdobje].plot(ax=axes[1])

plt.show()
以下是在数据框中导入的数据:

         Adj Close
Date                  
2015-12-01  73912.6016
2015-11-02  75638.3984
2015-10-01  79409.0000
2015-09-01  74205.5000
2015-08-03  75210.3984

           Location       CLI
TIME                         
1957-12-01      GBR  98.06755
1958-01-01      GBR  98.09290
1958-02-01      GBR  98.16694
1958-03-01      GBR  98.27734
1958-04-01      GBR  98.40984
我得到的结果是:

所以,问题是,X轴不共享。它们很接近,但不是共享的。有什么建议可以解决这个问题吗?我尝试了
sharex=True
,但Python每次都崩溃了

提前谢谢各位


向您致意,David

您可能需要将最终数据帧重新索引为所有数据帧的并集<启用
sharex=True
时,code>matplotlib将最后一个子批次的x轴作为整个绘图的轴。这会让你过得很好

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

fig, axes = plt.subplots(nrows=2,
                         ncols=1,
                         sharex=True)

df1 = pd.DataFrame(
    data = np.random.rand(25, 1),
    index=pd.date_range('2015-05-05', periods=25),
    columns=['DF1']
)

df2 = pd.DataFrame(
    data = np.random.rand(25, 1),
    index=pd.date_range('2015-04-10', periods=25),
    columns=['DF2']
)

df3 = pd.DataFrame(
    data = np.random.rand(50, 1),
    index=pd.date_range('2015-03-20', periods=50),
    columns=['DF3']
)

df3 = df3.reindex(index=df3.index.union(df2.index).union(df1.index))

df1.plot(ax=axes[0], linewidth=2, color='b', linestyle='solid')
df2.plot(ax=axes[0], linewidth=2, color='b', linestyle='dashed')
df3.plot(ax=axes[1])

plt.show()
生产这个,,


如您所见,轴现在已对齐。

谢谢,现在可以工作了。但我有一个新问题。在哪里设置特定日期或更有用,如何在绘图中设置期间?假设我只想观察过去6个月,12个月?@DavidV你可以通过简单的操作(在我上面的例子中):
df3[-30:].plot(ax=axes[1])
来切下你的
df
。那只会勾勒出过去三十天的情节。:)再次感谢。:)它本该起作用的。我还尝试用每日数据(首先我使用每月数据)更改一个数据帧,然后所有数据都被删除。如何以与上述相同的方式绘制一个包含月度数据的数据帧和第二个包含每日数据的数据帧?我知道带句点的简单解决方案是行不通的;)但我也知道我没有得到我应该得到的相关结果。建议?@DavidV为新问题打开一个新问题。评论不是回答多个问题的地方,因为将来有类似问题的其他用户可能无法在此处找到解决方案。