-
-
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 10 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,21 @@ | ||
``ax.table`` will accept a pandas dataframe | ||
-------------------------------------------- | ||
|
||
The `~Axes.axes.table` method can now accept a data frame for the ``cellText`` method, which | ||
anijjar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
it attempts to render with column headers set by ``df.columns.to_numpy()`` and cell data set by ``df.to_numpy()``. | ||
anijjar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. 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 @@ def table(ax, | |
|
||
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,13 @@ def table(ax, | |
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 |
||
# Convert to numpy array | ||
header = cellText.columns.to_numpy() | ||
data = cellText.to_numpy() | ||
cellText = np.vstack([header, data]) | ||
|
||
rows = len(cellText) | ||
cols = len(cellText[0]) | ||
for row in cellText: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,12 @@ from .transforms import Bbox | |
from .typing import ColorType | ||
|
||
from collections.abc import Sequence | ||
from typing import Any, Literal | ||
from typing import Any, Literal, TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
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. Having an This does introduce a type-check time requirement on 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'll reiterate that I don't think we should be special casing pandas at all, and we should not have a pandas dataframe as a type annotation. Is there anywhere else in the library that we do this? 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. Should I import the DataFrame method directly? |
||
from pandas import DataFrame | ||
else: | ||
DataFrame = None | ||
timhoffm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
class Cell(Rectangle): | ||
PAD: float | ||
|
@@ -68,7 +73,7 @@ class Table(Artist): | |
|
||
def table( | ||
ax: Axes, | ||
cellText: Sequence[Sequence[str]] | None = ..., | ||
cellText: Sequence[Sequence[str]] | DataFrame | None = ..., | ||
cellColours: Sequence[Sequence[ColorType]] | None = ..., | ||
cellLoc: Literal["left", "center", "right"] = ..., | ||
colWidths: Sequence[float] | None = ..., | ||
|
Uh oh!
There was an error while loading. Please reload this page.