mwaskom / seaborn

Statistical data visualization in Python
https://seaborn.pydata.org
BSD 3-Clause "New" or "Revised" License
12.59k stars 1.93k forks source link

Is it possible to config seaborn to follow mpl behavior to break line plot with nan value? #3785

Closed randomseed42 closed 1 week ago

randomseed42 commented 1 week ago

Is it possible to config seaborn to follow mpl behavior to break line plot with nan value? Some times it is useful, and this is also default for other plot tools like echarts.

Refer Plotting masked and NaN values

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-np.pi/2, np.pi/2, 31)
y = np.cos(x)**3

# 1) remove points where y > 0.7
x2 = x[y <= 0.7]
y2 = y[y <= 0.7]

# 2) mask points where y > 0.7
y3 = np.ma.masked_where(y > 0.7, y)

# 3) set to NaN where y > 0.7
y4 = y.copy()
y4[y3 > 0.7] = np.nan

fig, ax = plt.subplots(figsize=(10, 8), nrows=2)
ax[0].plot(x*0.1, y, 'o-', color='lightgrey', label='No mask')
ax[0].plot(x2*0.4, y2, 'o-', label='Points removed')
ax[0].plot(x*0.7, y3, 'o-', label='Masked values')
ax[0].plot(x*1.0, y4, 'o-', label='NaN values')
ax[0].legend()
ax[0].set_title('Masked and NaN data in Matplotlib')

sns.lineplot(x=x*0.1, y=y, marker='o', color='lightgrey', label='No mask', ax=ax[1])
sns.lineplot(x=x2*0.4, y=y2, marker='o', label='Points removed', ax=ax[1])
sns.lineplot(x=x*0.7, y=y3, marker='o', label='Masked values', ax=ax[1])
sns.lineplot(x=x*1.0, y=y4, marker='o', label='NaN values', ax=ax[1])
ax[1].legend()
ax[1].set_title('Masked and NaN data in Seaborn')

plt.tight_layout()
plt.show()

image

mwaskom commented 1 week ago

Not currently; duplicate of https://github.com/mwaskom/seaborn/issues/1552