-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Feature: Support passing DataFrames to table.table #28830
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b307395
7cb26b1
10d69f2
f73fa6a
89f79cb
e919949
193a64f
e73c8ae
80a2390
f713806
707bc72
dbcb91f
1946284
ef7d139
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
``ax.table`` will accept a pandas DataFrame | ||
-------------------------------------------- | ||
|
||
The `~.axes.Axes.table` method can now accept a Pandas DataFrame for the ``cellText`` argument. | ||
|
||
.. code-block:: python | ||
|
||
import matplotlib.pyplot as plt | ||
import pandas as pd | ||
|
||
data = { | ||
'Letter': ['A', 'B', 'C'], | ||
'Number': [100, 200, 300] | ||
} | ||
|
||
df = pd.DataFrame(data) | ||
fig, ax = plt.subplots() | ||
table = ax.table(df, loc='center') # or table = ax.table(cellText=df, loc='center') | ||
ax.axis('off') | ||
plt.show() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,8 @@ | |
from .transforms import Bbox | ||
from .path import Path | ||
|
||
from .cbook import _is_pandas_dataframe | ||
|
||
|
||
class Cell(Rectangle): | ||
""" | ||
|
@@ -674,7 +676,7 @@ | |
|
||
Parameters | ||
---------- | ||
cellText : 2D list of str, optional | ||
cellText : 2D list of str or pandas.DataFrame, optional | ||
The texts to place into the table cells. | ||
|
||
*Note*: Line breaks in the strings are currently not accounted for and | ||
|
@@ -744,6 +746,21 @@ | |
cols = len(cellColours[0]) | ||
cellText = [[''] * cols] * rows | ||
|
||
# Check if we have a Pandas DataFrame | ||
if _is_pandas_dataframe(cellText): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd actually make this more generic, and check if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that As a side-remark: The table implementation has lots of design problems. If one was serious about tables, the whole |
||
# if rowLabels/colLabels are empty, use DataFrame entries. | ||
# Otherwise, throw an error. | ||
if rowLabels is None: | ||
rowLabels = cellText.index | ||
else: | ||
raise ValueError("rowLabels cannot be used alongside Pandas DataFrame") | ||
if colLabels is None: | ||
colLabels = cellText.columns | ||
else: | ||
raise ValueError("colLabels cannot be used alongside Pandas DataFrame") | ||
# Update cellText with only values | ||
cellText = cellText.values | ||
|
||
rows = len(cellText) | ||
cols = len(cellText[0]) | ||
for row in cellText: | ||
|
Uh oh!
There was an error while loading. Please reload this page.