Matplotlib.pyplot-对数对数图上的多个图将不显示第三个图

时间:2019-02-24 04:23:00

标签: python matplotlib

以下内容处理前两个图:

data = [228782,38376,1416068,15177,3267,37238,43946,16882,1032,18015,32867,99886,10837,539578,2097,243504,721254,821,7264,24889,11301229,45200,1489405,106374,86755,117564,1195884,35285,1,1,69718,49305,99705,755060,172177,90544,375,13231,41636,2194328,445222,4151167,16397,13233,16167,28013,23123,20600,279078,204561,903049,1077988,3175300,133098,313077,150900,7916,7192,27825,11757,131951,36491,412661,13770,234963,14,27165,2655993,35906,4726736,1400946,50211,970,12991,500415,49638,227901,113491,34705,3601896,13206,2,163363,1763238,704929,17683,3345746,6169795,50960,10562405,19964,8768,60654,36688,408987,5491596,9198,3555,6956,44754,24177,44582,961052,53915,136564,88098,2506,4963972,30797,182620,120865,255538,82780,62005,31621,1878092,9538,3,104934,190948,32669,144033,1476,10792,490043,44086,16947,65156,4,67559,91119,158176,41496,444282,716632,370458,3497,113392,14303,14122,29358,35527,3359,1283,49466,964715,2231204,222141,215161,295728,32342,13462,2479807,5400340,388405,67750,22347,1491,106668,1533418,535,10953,10796,286274,33799,68004,11507,84894,353376,963503,2450530,161553,146589,796,94487,124304,4909631,31693,7574328,321190,15335,243789,71551,4162413,292380,140728,611758,536,120032,549,32414,52314,3321,5251,17321,290913,224,138860,2175516,13886,3674482,308513,2179765,11830,2228414,128376,103459,100288,442028,2685164,176,2,821791,780,533,125481,140268,96293,223237,22322,51114,10292,126129,271666,104450,5073589,1767339,250,32306,154856,20439,2160830,5,310789,896015,754529,38198,1445987,12020931,795411,25003,538,36861,18150,991877,1962984,48752,82654,3963056,6494512,79644,12438,20884,7,3849311,1495,6469,291234,72614,5439,26130,274373,12597,811805,3,295383,99982,22564,38928,92,1907481,3075,729,658168,1165,951107,128879,680182,3601,4208,2026,108807,746024,2866765,1505305,344209,223629,121982,77107,20725,12501,6308,858843,3675204,5050872,152005,36862,238924,52329,2049905,29376,855373,3766990,1756,1516744,22267,1515269,616,10687,17424,30983,314870,5017553,110463,50482,34061,33543,107524,619803,1108,190765,108684,2452800,90389,3213,871491,3760,773869,63341,5691,23539,20696,36256,373034,8614,4724,3692,13870,105831,26373,3188,160035,27253,1281,3332,43168]

rank = sorted(list(range(1, len(data)+1)))
freq = np.array(sorted(data, reverse=True))
plt.figure(figsize=(8, 6))
plt.xlim(1, 10**3)
plt.ylim(1, 10**8)
lines = plt.loglog(rank, freq, marker=".")
plt.plot([1, freq[0]], [freq[0], 1], 'r')

现在我正在尝试添加以下对数正态图:

    # log-normal
x = np.ma.log(freq)
avg = np.mean(x)
std = np.std(x)
pdf = lognorm.pdf(x, avg, 0, np.exp(std))
plt.loglog(pdf, x, marker='v')
# plt.plot(pdf, x, 'gv-')

title('Zipf plot of Airport frequency')
xlabel('frequency rank')
ylabel('airport frequency')
grid(True)
plt.show()

这是两个代码块产生的结果,如您所见,第三幅图未显示:

enter image description here 感谢有关如何使它起作用的任何见解。我使用带有最新版本的matplotlib和其他软件包的Python 3.7在Win10上运行。

1 个答案:

答案 0 :(得分:0)

实际上都是在绘制两个图,但是您使用的x和y限制将忽略对数正态数据。我完全复制并运行了您的代码,除了将限制更改为

plt.xlim(10**-3, 10**3)
plt.ylim(10**-1, 10**8)

这产生了下面的情节

enter image description here

当我想调整图的x和y极限时,通常会在plt.show()之前或定义xlabelylabel时结束。我还将执行min(xdata)max(xdata)来获取显示所有数据的限制(也可以根据需要添加偏移量)。