0

I am looking for a way to plot multiple histograms of 2D data, but I could not find any documentation about addressing such a problem.

Plotting multiple 1-dimensional histograms on a same chart with Matplotlib has been addressed in this thread and it also covered in the Matplotlib docs.

There are threads and documentations that deal with 2D data (3d plots or contours), but for a single data series.

I could not find any thread/docs on plotting multiple 2D-data histograms on the same chart. Is there a way to do it?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
SomethingSomething
  • 11,491
  • 17
  • 68
  • 126
  • 1
    Seaborn's `sns.kdeplot` can work with 2D data showing normalized histograms and use hue to distinguish data. See the geyser examples at https://seaborn.pydata.org/generated/seaborn.kdeplot.html – JohanC May 17 '23 at 08:50

1 Answers1

1

I am not certain I got the question right, but I don't have any issue with that. Taken from this demo:

import numpy as np
import matplotlib.pyplot as plt

# set up the figure and axes
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(111, projection='3d')

n_fake_data = 2

for _ in range(n_fake_data):
    
    # fake data
    _x = np.random.randint(0, 10, 4)
    _y = np.random.randint(0, 10, 4)
    _xx, _yy = np.meshgrid(_x, _y)
    x, y = _xx.ravel(), _yy.ravel()

    top = x + y
    bottom = np.zeros_like(top)
    width = depth = 1

    ax.bar3d(x, y, bottom, width, depth, top, shade=True)
    

plt.show()   

And the result: Stacked 3D histograms

Of course, you would have to play with depth / transparency to see both histograms.

Syla
  • 131
  • 3