darkpool
darkpool

Reputation: 14661

Creating a subplot instead of separate plots

I have the following dataframe:

symbol     aaa       bbb      ccc     ddd      eee     fff      ggg
aaa                         
bbb       -0.001                        
ccc        0.348    -0.025                  
ddd       -0.42     -0.075   -0.701             
eee       -0.276     0.004   -0.516   0.661         
fff        0.175    -0.107    0.363  -0.521   -0.356        
ggg        0.469     0.012    0.364  -0.519   -0.306    0.306   

I am creating two separate plots based on the above dataframe as follows:

def plot_bar(self, corr_df):

    dfstacked = corr_df.stack().order()
    dfstacked.plot(kind='bar', rot=60)
    plt.show()

def plot_heatmap(self, corr_df):

    corr_df = corr_df.fillna(value=0)
    plt.pcolormesh(corr_df.values, cmap=plt.cm.Blues)
    plt.yticks(np.arange(0.5, len(corr_df.index), 1), corr_df.index)
    plt.xticks(np.arange(0.5, len(corr_df.columns), 1), corr_df.columns)
    plt.show()

Bar plot color mesh plot

Now what I need to do is instead of plotting these two charts separately, I need them together in a grid. I have managed to create the following subplot which needs to have the bar chart on the left, and the matrix/color mesh on the right:

fig, axes = plt.subplots(2)
axes[0] = plt.subplot2grid((1,5), (0,0), colspan=3)
axes[1] = plt.subplot2grid((1,5), (0,3), colspan=2)
plt.show()

grid

For the life of me I can't seem to get this working. I need the bar chart on axes[0] and the matrix/color mesh on axes[1]

Upvotes: 1

Views: 384

Answers (1)

furas
furas

Reputation: 143098

Did you try

fig, axes = plt.subplots(2)

plt.subplot2grid((1,5), (0,0), colspan=3)
# here plot something

plt.subplot2grid((1,5), (0,3), colspan=2)
# here plot something

plt.show()

for example

import matplotlib.pyplot as plt

fig, axes = plt.subplots(2)

plt.subplot2grid((1,5), (0,0), colspan=3)
plt.plot([1,2,3]) # plot something

plt.subplot2grid((1,5), (0,3), colspan=2)
plt.plot([1,2,1]) # plot something

plt.show()

EDIT:

import pandas as pd
import numpy as np

def plot_bar(corr_df):

    dfstacked = corr_df.stack().order()
    dfstacked.plot(kind='bar', rot=60)

def plot_heatmap(corr_df):

    corr_df = corr_df.fillna(value=0)
    plt.pcolormesh(corr_df.values, cmap=plt.cm.Blues)
    plt.yticks(np.arange(0.5, len(corr_df.index), 1), corr_df.index)
    plt.xticks(np.arange(0.5, len(corr_df.columns), 1), corr_df.columns)


df = pd.DataFrame(range(10))

fig, axes = plt.subplots(2)

plt.subplot2grid((1,5), (0,0), colspan=3)
plot_bar(df)

plt.subplot2grid((1,5), (0,3), colspan=2)
plot_heatmap(df)

plt.show()

http://i.imgur.com/wSiT1UP.png

Upvotes: 3

Related Questions