Skip to content

Suggest to add kde to histogram plot. #794

Description

@gepcel

I noticed that you mentioned something about this earlier in here that:

In the future, UltraPlot will include options for adding “smooth” kernel density estimations to histograms plots using a kde keyword. It will also include separate ultraplot.axes.PlotAxes.kde and ultraplot.axes.PlotAxes.kde2d commands. The violin() and violinh() commands will use the same algorithm for kernel density estimation as the kde commands.

Adding all kinds of statistical plots to all plotting methods would be a huge task, so perhaps we can tackle it bit by bit. Currently, I'm working on some plots that require KDE for histograms. I've opened a pull request for you to review.

With this pr, make the following examples:

# %% Example 1
import numpy as np
import ultraplot as uplt
import seaborn as sns
# Sample data
M, N = 50, 3
state = np.random.RandomState(51423)
x = state.normal(size=(M, N)) + state.rand(M)[:, None] * np.arange(N) + 2 * np.arange(N)
# Sample overlayed histograms
fig, ax = uplt.subplots(ncols=2, nrows=2, refwidth=4, refaspect=(3, 2))
ax[0, :].format(title="Overlaid histograms with density=False", xlabel="distribution", ylabel="count")
ax[1, :].format(title="Overlaid histograms with density=True", xlabel="distribution", ylabel="density")
ax[0].hist(
x, uplt.arange(-3, 8, 0.2), filled=True, alpha=0.7, edgecolor="k",
cycle=("indigo9", "gray3", "red9"), labels=list("abc"),
legend="ul", kde=True)
with ax[1].external():
sns.histplot(x, kde=True, bins=np.arange(-3, 8, 0.2), ax=ax[1])
ax[2].hist(x[:,0], bins=10, density=True, kde=True,ec='white')
with ax[3].external():
sns.histplot(x[:,0], kde=True, bins=10,ax=ax[3],stat='density')
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions