从matplotlib绘制loglog图时,“无法将浮点无穷大转换为整数”

时间:2011-10-16 16:39:15

标签: python matplotlib divide-by-zero

我需要从数据中绘制一些对数图(值已经是对数刻度)但是matplotlib在准备loglog图时会以某种方式遇到无穷大。

我相信在is_decade函数中某些值接近0时,除以0会出现:

def is_decade(x,base=10):
    lx = math.log(x)/math.log(base)
    return lx==int(lx)

任何想法如何处理这个问题? 这是我的数据集的代码示例。

import matplotlib.pyplot as pyplot

list_x = [-0.5, -0.47979798913002014, -0.45959600806236267, -0.43939390778541565, -0.41919189691543579, -0.39898988604545593, -0.37878790497779846, -0.3585858941078186, -0.33838379383087158, -0.31818181276321411, -0.29797980189323425, -0.27777779102325439, -0.25757580995559692, -0.23737369477748871, -0.21717169880867004, -0.19696970283985138, -0.17676770687103271, -0.15656569600105286, -0.13636359572410583, -0.11616159975528717, -0.095959596335887909, -0.075757578015327454, -0.055555559694766998, -0.035353541374206543, -0.015151520259678364, 0.0050505050458014011, 0.025252530351281166, 0.045454550534486771, 0.065656572580337524, 0.08585859090089798, 0.10606060177087784, 0.1262626051902771, 0.14646460115909576, 0.16666670143604279, 0.18686869740486145, 0.20707069337368011, 0.22727270424365997, 0.24747470021247864, 0.26767680048942566, 0.28787881135940552, 0.30808079242706299, 0.32828280329704285, 0.34848481416702271, 0.36868691444396973, 0.3888888955116272, 0.40909090638160706, 0.42929288744926453, 0.44949489831924438, 0.46969699859619141, 0.48989900946617126, 0.51010102033615112, 0.53030300140380859, 0.55050510168075562, 0.57070708274841309, 0.59090912342071533, 0.6111111044883728, 0.63131308555603027, 0.65151518583297729, 0.67171722650527954, 0.69191920757293701, 0.71212118864059448, 0.73232322931289673, 0.75252532958984375, 0.77272731065750122, 0.79292929172515869, 0.81313127279281616, 0.83333331346511841, 0.85353541374206543, 0.8737373948097229, 0.89393937587738037, 0.91414141654968262, 0.93434339761734009, 0.95454549789428711, 0.97474747896194458, 0.99494951963424683, 1.0151499509811401, 1.03534996509552, 1.0555599927902222, 1.0757600069046021, 1.0959600210189819, 1.1161600351333618, 1.1363600492477417, 1.1565699577331543, 1.1767699718475342, 1.1969699859619141, 1.2171700000762939, 1.2373700141906738, 1.257580041885376, 1.2777800559997559, 1.2979799509048462, 1.3181799650192261, 1.338379979133606, 1.3585900068283081, 1.378790020942688, 1.3989900350570679, 1.4191900491714478, 1.4393899440765381, 1.4595999717712402, 1.4797999858856201, 1.5, 1.5202000141143799, 1.5404000282287598, 1.5606000423431396, 1.5808000564575195, 1.6009999513626099, 1.6211999654769897, 1.6413999795913696, 1.6615999937057495, 1.6818000078201294, 1.7020000219345093, 1.7222000360488892, 1.742400050163269, 1.7625999450683594, 1.7827999591827393, 1.8029999732971191, 1.823199987411499, 1.8434000015258789, 1.8636000156402588, 1.8838000297546387, 1.9040000438690186]
list_y = [-0.033239554613828659, -0.037707798182964325, -0.042734216898679733, -0.048379786312580109, -0.054710298776626587, -0.061796598136425018, -0.069714449346065521, -0.078544475138187408, -0.088371939957141876, -0.099286220967769623, -0.11138062179088593, -0.12475139647722244, -0.13949693739414215, -0.15571676194667816, -0.17350961267948151, -0.19297224283218384, -0.21419693529605865, -0.23726895451545715, -0.26226347684860229, -0.28924143314361572, -0.31824567914009094, -0.34929537773132324, -0.38237985968589783, -0.41745156049728394, -0.4544181227684021, -0.49313366413116455, -0.5333896279335022, -0.57490617036819458, -0.61732500791549683, -0.66020554304122925, -0.70302659273147583, -0.74519622325897217, -0.78607243299484253, -0.82499557733535767, -0.86133092641830444, -0.89451992511749268, -0.92412877082824707, -0.94988745450973511, -0.97171056270599365, -0.98969388008117676, -1.0040895938873291, -1.0152649879455566, -1.0236542224884033, -1.0297127962112427, -1.0338805913925171, -1.0365564823150635, -1.0380839109420776, -1.0387473106384277, -1.0387735366821289, -1.0383385419845581, -1.0375756025314331, -1.0365840196609497, -1.0354362726211548, -1.034185528755188, -1.0328699350357056, -1.0315178632736206, -1.0301498174667358, -1.0287811756134033, -1.0274238586425781, -1.0260865688323975, -1.0247762203216553, -1.0234981775283813, -1.0222563743591309, -1.0210539102554321, -1.0198931694030762, -1.0187755823135376, -1.0177022218704224, -1.0166736841201782, -1.0156899690628052, -1.0147509574890137, -1.0138561725616455, -1.0130050182342529, -1.0121965408325195, -1.0114297866821289, -1.0107033252716064, -1.0100163221359253, -1.0093671083450317, -1.0087541341781616, -1.0081764459609985, -1.0076324939727783, -1.0071206092834473, -1.0066394805908203, -1.0061874389648438, -1.0057634115219116, -1.0053658485412598, -1.0049934387207031, -1.0046447515487671, -1.0043183565139771, -1.0040134191513062, -1.0037285089492798, -1.0034624338150024, -1.0032142400741577, -1.0029826164245605, -1.0027667284011841, -1.0025657415390015, -1.0023785829544067, -1.0022042989730835, -1.0020421743392944, -1.0018914937973022, -1.0017514228820801, -1.0016212463378906, -1.0015003681182861, -1.0013881921768188, -1.0012840032577515, -1.0011874437332153, -1.0010977983474731, -1.0010148286819458, -1.000937819480896, -1.0008665323257446, -1.0008004903793335, -1.0007392168045044, -1.0006827116012573, -1.000630259513855, -1.0005817413330078, -1.0005369186401367, -1.000495433807373, -1.0004570484161377, -1.0004216432571411, -1.0003888607025146, -1.0003585815429688]

pyplot.loglog(list_x, list_y, basex=10, basey=10)

1 个答案:

答案 0 :(得分:1)

您的数据是负数,对于loglog图,它必须是正数。