matplotlib 中的背靠背直方图

发布于 2024-08-02 14:49:12 字数 174 浏览 2 评论 0原文

Matlab 有一个很好的函数可以绘制背对背直方图。我需要在 matplotlib 中创建一个类似的图表。谁能展示一个有效的代码示例?

There is a nice function that draws back to back histograms in Matlab. I need to create a similar graph in matplotlib. Can anyone show a working code example?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡淡绿茶香 2024-08-09 14:49:12

感谢 Mark Rushakoff 指出的链接,以下是我最终所做的

import numpy as np
from matplotlib import pylab as pl

dataOne = get_data_one()
dataTwo = get_data_two()

hN = pl.hist(dataTwo, orientation='horizontal', normed=0, rwidth=0.8, label='ONE')
hS = pl.hist(dataOne, bins=hN[1], orientation='horizontal', normed=0, 
    rwidth=0.8, label='TWO')

for p in hS[2]:
    p.set_width( - p.get_width())

xmin = min([ min(w.get_width() for w in hS[2]), 
                min([w.get_width() for w in hN[2]]) ])
xmin = np.floor(xmin)
xmax = max([ max(w.get_width() for w in hS[2]), 
                max([w.get_width() for w in hN[2]]) ])
xmax = np.ceil(xmax)
range = xmax - xmin
delta = 0.0 * range
pl.xlim([xmin - delta, xmax + delta])
xt = pl.xticks()
n = xt[0]
s = ['%.1f'%abs(i) for i in n]
pl.xticks(n, s)
pl.legend(loc='best')
pl.axvline(0.0)
pl.show()

Thanks to the link pointed by Mark Rushakoff, following is what I finally did

import numpy as np
from matplotlib import pylab as pl

dataOne = get_data_one()
dataTwo = get_data_two()

hN = pl.hist(dataTwo, orientation='horizontal', normed=0, rwidth=0.8, label='ONE')
hS = pl.hist(dataOne, bins=hN[1], orientation='horizontal', normed=0, 
    rwidth=0.8, label='TWO')

for p in hS[2]:
    p.set_width( - p.get_width())

xmin = min([ min(w.get_width() for w in hS[2]), 
                min([w.get_width() for w in hN[2]]) ])
xmin = np.floor(xmin)
xmax = max([ max(w.get_width() for w in hS[2]), 
                max([w.get_width() for w in hN[2]]) ])
xmax = np.ceil(xmax)
range = xmax - xmin
delta = 0.0 * range
pl.xlim([xmin - delta, xmax + delta])
xt = pl.xticks()
n = xt[0]
s = ['%.1f'%abs(i) for i in n]
pl.xticks(n, s)
pl.legend(loc='best')
pl.axvline(0.0)
pl.show()
暮年慕年 2024-08-09 14:49:12

这个 matplotlib 用户邮件帖子 有一些示例向上和向下而不是向左和向右移动的双直方图的代码。 这是他链接到的示例输出

如果上下绝对不适合您,那么只需几分钟即可将 y 轴操作与 x 轴操作交换。

另外,您的链接不是 MATLAB 函数,而是某人编写的大约 40 行的实际脚本。您实际上可以查看脚本源并尝试移植它,因为 MATLAB 和 matplotlib 具有相当接近的语法。

This matplotlib users mailing post has some sample code for a bihistogram that goes up and down instead of left and right. Here's the example output he linked to.

If up-down absolutely won't work for you, it should only take a few minutes to swap the operations on the y-axis with the x-axis operations.

Also, your link isn't a MATLAB function, it's an actual script that someone wrote in about 40 lines. You could actually look at the script source and try porting it, since MATLAB and matplotlib have fairly close syntax.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文