<p>为了使条形图的总高度保持在对数刻度上,但条形图中类别的比例是线性的,可以重新计算堆积数据,使其在对数刻度上呈线性。在</p>
<p>作为一个示例,让我们选择6个具有非常不同总计(<code>[5,10,50,100,500,1000]</code>)的数据集,这样在线性比例下,较低的条形图将非常小。让我们把它分成30%、50%和20%的部分(为了简单起见,所有不同的数据按相同的比例划分)。在</p>
<p>然后,我们可以计算出每个数据点的大小,这些数据点以后应该出现在堆积条形图上,这样30%、50%和20%的比率保留在对数比例图中,最后绘制这些新创建的数据。在</p>
<pre><code>from __future__ import division
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
a = np.array([5,10,50,100,500,1000])
p = [0.3,0.5,0.2]
c = np.c_[p[0]*a,p[1]*a, p[2]*a]
d = np.zeros(c.shape)
for j, row in enumerate(c):
g = np.zeros(len(row)+1)
G = np.sum(row)
g[1:] = np.cumsum(row)
f = 10**(g/G*np.log10(G))
f[0] = 0
d[j, :] = np.diff( f )
collabels = ["{:3d}%".format(int(100*i)) for i in p]
dfo = pd.DataFrame(c, columns=collabels)
df2 = pd.DataFrame(d, columns=collabels)
fig, axes = plt.subplots(ncols=2)
axes[0].set_title("linear stack bar")
dfo.plot.bar(stacked=True, log=False, ax=axes[0])
axes[0].set_xticklabels(a)
axes[1].set_title("log total barheight\nlinear stack distribution")
df2.plot.bar(stacked=True, log=True, ax=axes[1])
axes[1].set_xticklabels(a)
axes[1].set_ylim([1, 1100])
plt.show()
</code></pre>
<p><a href="https://i.stack.imgur.com/yRTzt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yRTzt.png" alt="enter image description here"/></a></p>
<p>最后一句话:我认为这样的情节应该小心。它可能对检查有用,但我不建议向其他人展示这样一个图,除非你能确保他们完全理解所画的内容和如何阅读它。否则,这可能会引起很多混乱,因为堆叠的类别的高度与比例不匹配,这只是一个错误。显示虚假数据会带来很多麻烦!在</p>