turtle_in_mind
turtle_in_mind

Reputation: 1152

creating two plots side by side in matplotlib

I'm having trouble plotting a data frame and a circle next to one another. My code:

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
df = \
pd.DataFrame({'data': {'Performance': "[None, ' bps']Performance cannot be calculated",
      'Basket Switch Cost': '400000 bps / 4000',
      '10\\% VWAP Switch Cost': '0 bps / 0',
      'Portfolio Expense Ratio': 'Savings of None bps  /  None',
      'Common Items': '523 Items  \\&  53% by Weight',
      'Starting \\& Ending Security Count': '611 / 611',
      'Largest Sector Exposure Difference': '0%  Increase  in Information Technology',
      'Common Inception Date': '2011-03-24'}})

ax = plt.subplot2grid((1,3), (0,0), colspan=2)

circle = plt.Circle((0.0,0.0),radius=0.75, fc='r')
plt.gca().add_patch(circle)
ax.axis('scaled')
ax2 = plt.subplot2grid((1,3), (0,2))
font_size=10
bbox=[0, 0, 1, 1]
ax2.axis('off')
mpl_table = ax2.table(cellText = df.values, rowLabels = df.index, 
                      bbox=bbox, colLabels=df.columns)
mpl_table.auto_set_font_size(False)
mpl_table.set_fontsize(font_size)
plt.tight_layout()

As you can see, the two figures are overlapping one another and I want the circle in the first column and the data frame in the 2nd. How can I accomplish this?

Upvotes: 0

Views: 100

Answers (1)

William Miller
William Miller

Reputation: 10320

You can get the desired display if you remove tight_layout(), adjust the bounding box for the table and the figure size, and then change the column spanning, i.e.

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
df = \
pd.DataFrame({'data': {'Performance': "[None, ' bps']Performance cannot be calculated",
            'Basket Switch Cost': '400000 bps / 4000',
            '10\\% VWAP Switch Cost': '0 bps / 0',
            'Portfolio Expense Ratio': 'Savings of None bps  /  None',
            'Common Items': '523 Items  \\&  53% by Weight',
            'Starting \\& Ending Security Count': '611 / 611',
            'Largest Sector Exposure Difference': '0%  Increase  in Information Technology',
            'Common Inception Date': '2011-03-24'}})

fig = plt.figure(figsize=(16,6))
ax = plt.subplot2grid((1,2), (0,0))

circle = plt.Circle((0.0,0.0),radius=0.75, fc='r')
ax.add_patch(circle)
ax.axis('scaled')
ax2 = plt.subplot2grid((1,2), (0,1))
font_size=10
bbox=[0.3, 0, 0.95, 1]
ax2.axis('off')
mpl_table = ax2.table(cellText = df.values, rowLabels = df.index, 
                      bbox=bbox, colLabels=df.columns)
mpl_table.auto_set_font_size(False)
mpl_table.set_fontsize(font_size)

Which would give you

enter image description here

Upvotes: 1

Related Questions