python中的对数y轴箱 [英] Logarithmic y-axis bins in python

查看:138
本文介绍了python中的对数y轴箱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建数据列的直方图并对其进行对数绘制(y-axis),但不确定以下代码为何不起作用:

I'm trying to create a histogram of a data column and plot it logarithmically (y-axis) and I'm not sure why the following code does not work:

import numpy as np
import matplotlib.pyplot as plt
data = np.loadtxt('foo.bar')
fig = plt.figure()
ax = fig.add_subplot(111)
plt.hist(data, bins=(23.0, 23.5,24.0,24.5,25.0,25.5,26.0,26.5,27.0,27.5,28.0))
ax.set_xlim(23.5, 28)
ax.set_ylim(0, 30)
ax.grid(True)
plt.yscale('log')
plt.show()

我也尝试过在plt.hist行中添加Log=true而不是plt.yscale('log'),也尝试了ax.set_yscale('log'),但是似乎没有任何效果.我要么得到一个空图,要么y-axis确实是对数的(使用上面显示的代码),但是没有绘制数据(没有bin).

I've also tried instead of plt.yscale('log') adding Log=true in the plt.hist line and also I tried ax.set_yscale('log'), but nothing seems to work. I either get an empty plot, either the y-axis is indeed logarithmic (with the code as shown above), but there is no data plotted (no bins).

推荐答案

尝试

plt.yscale('log', nonposy='clip')

http://matplotlib.org/api/pyplot_api.html#matplotlib. pyplot.yscale

问题在于小节的底部位于y = 0,默认值是在进行对数转换时掩盖了无效点(log(0)-> undefined)(有人在讨论更改此问题,不记得它走了哪条路),所以当它尝试为您的条形图绘制矩形时,其底边被遮盖了->没有矩形.

The issue is with the bottom of bars being at y=0 and the default is to mask out in-valid points (log(0) -> undefined) when doing the log transformation (there was discussion of changing this, but I don't remember which way it went) so when it tries to draw the rectangles for you bar plot, the bottom edge is masked out -> no rectangles.

这篇关于python中的对数y轴箱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆