Common x axis in Pandas Python

Usually I always get the answer to my questions here, so here's a new one. I am working on some data analysis when I import various csv files, set the index, and then try to build it.

Here is the code. Keep in mind that I use obdobjeand -obdobjebecause the index comes from different files, but the format is the same:

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

Here are the data that is imported into the dataframe:

         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

And the result that I get: enter image description here

So the problem is that the X axes are not separated. They are close, but not shared. Any suggestions how to solve this? I tried with sharex=True, but Python crashed every time.

Thanks in advance guys.

Best regards, David

+4
source share
1

, . matplotlib x sharex=True. ,

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

, enter image description here

, .

+2

Source: https://habr.com/ru/post/1623488/


All Articles