在同一张图上绘制两个2D数据的直方图。

huangapple go评论73阅读模式
英文:

Plot two 2D-data histograms on the same chart

问题

我正在寻找一种绘制2D数据的多个直方图的方法,但我找不到关于解决这个问题的任何文档。

在Matplotlib上绘制多个1维直方图此主题中有讨论,并且在Matplotlib文档中也有涉及。

有一些主题和文档处理2D数据(3D图轮廓),但是针对单个数据系列

我找不到有关在同一图表上绘制多个2D数据直方图的任何主题/文档。有办法吗?

英文:

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.

<img src="https://i.stack.imgur.com/MzHP9.png" width="400" />

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

<img src="https://matplotlib.org/3.1.0/_images/sphx_glr_hist3d_001.png" width="300" />
<img src="https://i.stack.imgur.com/DGWz8.png" width="200" />

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

答案1

得分: 1

以下是翻译好的部分:

我不确定我理解问题的正确性,但我对此没有任何问题。
取自此演示

import numpy as np
import matplotlib.pyplot as plt

# 设置图形和坐标轴
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(111, projection='3d')

n_fake_data = 2

for _ in range(n_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()

和结果:
在同一张图上绘制两个2D数据的直方图。

当然,您需要调整深度/透明度以查看两个直方图。

英文:

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=&#39;3d&#39;)

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:
在同一张图上绘制两个2D数据的直方图。

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

huangapple
  • 本文由 发表于 2023年5月17日 16:09:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269866.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定