pd.NA reverses axis ordering
See original GitHub issueIssue
When plotting with pd.NA
, axis ordering get reversed into descending.
Workaround
np.nan
does not produce this issue
Expected Behavior
NAs should be excluded without reversing axis order
Reproducible Example
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
mock_data = pd.DataFrame({
'date': ['0', '1', '2', '3'],
'value': [1, 2, 1, 1.5]
})
mock_data_full = mock_data.copy()
mock_data_full['type'] = 'no_NA'
mock_data_pd_na = mock_data.copy()
mock_data_pd_na['type'] = 'pd.NA'
mock_data_pd_na.loc[2, 'value'] = pd.NA
mock_data_np_nan = mock_data.copy()
mock_data_np_nan['type'] = 'np.nan'
mock_data_np_nan.loc[2, 'value'] = np.nan
test_data = pd.concat([mock_data_full, mock_data_pd_na, mock_data_np_nan])
grid = sns.FacetGrid(
data=test_data,
col='type',
sharey=False,
sharex=True, # time-series consistency
)
grid.map(sns.lineplot, 'date', 'value', alpha=0.5)
plt.show()
Result
System Info
print(f'''
python: {sys.version},
seaborn: {sns.__version__},
pandas: {pd.__version__}
''')
python: 3.9.7 (default, Sep 9 2021, 23:20:13) [GCC 9.3.0],
seaborn: 0.11.2,
pandas: 1.3.4
Issue Analytics
- State:
- Created 2 years ago
- Comments:10 (5 by maintainers)
Top Results From Across the Web
How to plot x values to reverse order, when using both primary ...
Use the Layout ribbon > Axes drop-down and show the secondary X axis. Format the secondary X axis to show in reverse order....
Read more >pandas.DataFrame.sort_values — pandas 1.5.2 documentation
Axis to be sorted. ascendingbool or list of bool, default True. Sort ascending vs. descending. Specify list for multiple sort orders.
Read more >How to reverse axis order in Excel? - ExtendOffice
Reverse axis order in chart. There is an option in Format Axis dialog for reversing the axis order. 1. Right click the y...
Read more >9.2.6 The (Plot Details) Axis Tab - OriginLab
Orientation of Labels & Titles & Ticks group. Specify whether to apply the orientation of tick labels, ticks and axis titles for all...
Read more >Pandas Sort: Your Guide to Sorting Data in Python - Real Python
As a quick reminder, a DataFrame is a data structure with labeled axes for both rows and ... By passing False to ascending...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is a weird one! Let’s debug.
First thought, this has something to do with using
FacetGrid
directly (which is discouraged). But no, we can reproduce usingrelplot
:Let’s get rid of
FacetGrid
altogether, and set up the figure with matplotlib. Still there:Now we can focus on a single
lineplot
and simplify even further. Is it because the x values are strings? No:Hm,
What if we force that to numeric?
There we go. So why is this happening? Seaborn will invert the y axis if the y variable is categorical, and indeed, it thinks the y variable is categorical:
But that’s because of the object dtype:
Seaborn will introspect and object-typed series and consider it numeric if every element is subclass of
numbers.Number
:This is intended to allow object-typed series that mix int and
np.nan
. But whilenp.nan
is aNumber
,pd.NA
is not:So this is happening because seaborn thinks your y variable is categorical in the case where you are using
pd.NA
for missing.seaborn is not doing the wrong thing from a plotting perspective here, it is just treating the vector as categorical because a) it does not have a numeric dtype and b) not all of its elements are numbers.