是否可以使用 Get 和 Set 操作 matplotlib 直方图中的数据?
Is it possible to manipulate the data in a matplotlib histogram using Get and Set?
我有一个使用 matplotlib 制作的堆叠直方图。它当然有多个箱子(每个扇区),每个 bin/bar 进一步细分为子扇区(堆叠直方图)。
我想知道如何获取数据点,做一些数学运算(假设将每个 bin 除以它的总值),然后设置新的数据点。
我希望它如何工作:
import matplotlib.plt as plt
ax = plt.subplt(111)
h = ax.hist((subsector1,subsector2,subsector3), bins = 20, stacked=True)
y_data = h.get_yData
y_data 的形状类似于 20 x 3(箱子 x 子扇区)
new_y_data = y_data normalized by total on each bin
new_y_data 的形状也像 20 x 3,但每个 bin 的总和将为 1(或 100%)
new_h = h.set_yData(new_y_data)
new_h 看起来更像条形图,条形大小相同,但每个条形上的子行业分布不同..
在 python matplotlib 中这甚至可能吗?
当您只需要这些值时,使用 np.histogram
会更容易,它无需绘制即可进行相同的计算。
当你有价值观时,plt.bar
draws the directly without needing plt.hist
。
Pandas plot.bar
might be an alternative. Have a look at 与您的示例类似。
这是一些使用 np.histogram
和 plt.bar
的示例代码:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import PercentFormatter
subsector1 = np.clip(np.random.normal(70, 20, 400), 0, 100)
subsector2 = np.clip(np.random.normal(50, 20, 1000), 0, 100)
subsector3 = np.clip(np.random.normal(25, 20, 500), 0, 100)
num_bins = 20
x_min = np.min(np.concatenate([subsector1, subsector2, subsector3]))
x_max = np.max(np.concatenate([subsector1, subsector2, subsector3]))
bounds = np.linspace(x_min, x_max, num_bins + 1)
values = np.zeros((num_bins, 3))
for i, subsect in enumerate((subsector1, subsector2, subsector3)):
values[:, i], _ = np.histogram(subsect, bins=bounds)
with np.errstate(divide='ignore', invalid='ignore'):
values /= values.sum(axis=1, keepdims=True)
fig, ax = plt.subplots()
bottom = 0
for i in range(3):
plt.bar((bounds[:-1] + bounds[1:]) / 2, values[:, i], bottom=bottom, width=np.diff(bounds) * 0.8)
bottom += values[:, i]
plt.xlim(x_min, x_max)
plt.gca().yaxis.set_major_formatter(PercentFormatter(1.0))
plt.show()
我有一个使用 matplotlib 制作的堆叠直方图。它当然有多个箱子(每个扇区),每个 bin/bar 进一步细分为子扇区(堆叠直方图)。
我想知道如何获取数据点,做一些数学运算(假设将每个 bin 除以它的总值),然后设置新的数据点。
我希望它如何工作:
import matplotlib.plt as plt
ax = plt.subplt(111)
h = ax.hist((subsector1,subsector2,subsector3), bins = 20, stacked=True)
y_data = h.get_yData
y_data 的形状类似于 20 x 3(箱子 x 子扇区)
new_y_data = y_data normalized by total on each bin
new_y_data 的形状也像 20 x 3,但每个 bin 的总和将为 1(或 100%)
new_h = h.set_yData(new_y_data)
new_h 看起来更像条形图,条形大小相同,但每个条形上的子行业分布不同..
在 python matplotlib 中这甚至可能吗?
当您只需要这些值时,使用 np.histogram
会更容易,它无需绘制即可进行相同的计算。
当你有价值观时,plt.bar
draws the directly without needing plt.hist
。
Pandas plot.bar
might be an alternative. Have a look at
这是一些使用 np.histogram
和 plt.bar
的示例代码:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import PercentFormatter
subsector1 = np.clip(np.random.normal(70, 20, 400), 0, 100)
subsector2 = np.clip(np.random.normal(50, 20, 1000), 0, 100)
subsector3 = np.clip(np.random.normal(25, 20, 500), 0, 100)
num_bins = 20
x_min = np.min(np.concatenate([subsector1, subsector2, subsector3]))
x_max = np.max(np.concatenate([subsector1, subsector2, subsector3]))
bounds = np.linspace(x_min, x_max, num_bins + 1)
values = np.zeros((num_bins, 3))
for i, subsect in enumerate((subsector1, subsector2, subsector3)):
values[:, i], _ = np.histogram(subsect, bins=bounds)
with np.errstate(divide='ignore', invalid='ignore'):
values /= values.sum(axis=1, keepdims=True)
fig, ax = plt.subplots()
bottom = 0
for i in range(3):
plt.bar((bounds[:-1] + bounds[1:]) / 2, values[:, i], bottom=bottom, width=np.diff(bounds) * 0.8)
bottom += values[:, i]
plt.xlim(x_min, x_max)
plt.gca().yaxis.set_major_formatter(PercentFormatter(1.0))
plt.show()