создавая два сюжета бок о бок в matplotlib - PullRequest
0 голосов
/ 28 января 2020

У меня проблемы с построением фрейма данных и круга рядом друг с другом. Мой код:

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

Как видите, две фигуры перекрывают друг друга, и я хочу, чтобы в первом столбце были кружок, а во втором - фрейм данных. Как я могу выполнить sh это?

1 Ответ

1 голос
/ 28 января 2020

Вы можете получить желаемое отображение, если уберете tight_layout(), отрегулируете ограничивающую рамку для таблицы и размера рисунка, а затем измените охват столбцов, то есть

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)

Что даст вам

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...