Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shared x axes in Pandas Python

Tags:

python

pandas

Usually I always get an answer to my questions here, so here is a new one. I'm working on some data analysis where I import different csv files, set index and then I try to plot it.

Here is the code. Please be aware that I use obdobje and -obdobje because 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 is data that is imported in 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 output that I get is: enter image description here

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

Thanks in advance guys.

Best regards, David

like image 697
DavidV Avatar asked Jan 08 '16 21:01

DavidV


People also ask

How do you label axes in Pandas?

You can set the labels on that object. Or, more succinctly: ax. set(xlabel="x label", ylabel="y label") . Alternatively, the index x-axis label is automatically set to the Index name, if it has one.

What is axes in Pandas DataFrame?

A DataFrame object has two axes: “axis 0” and “axis 1”. “axis 0” represents rows and “axis 1” represents columns. Now it's clear that Series and DataFrame share the same direction for “axis 0” – it goes along rows direction.


1 Answers

You may want to reindex your final dataframe to a union of all data frames. matplotlib takes the x-axis of the last subplot as the axis of the entire plot when enabling sharex=True. This should get you along,

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

Produces this, enter image description here

As you can see, the axes are now aligned.

like image 197
Niels Wouda Avatar answered Nov 14 '22 06:11

Niels Wouda