英文:
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()
当然,您需要调整深度/透明度以查看两个直方图。
英文:
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()
Of course, you would have to play with depth / transparency to see both histograms.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论