简体   繁体   English

使用matplotlib在熊猫中以对数比例绘制x和y轴

[英]plotting both x and y axis in log scale in pandas using matplotlib

My trace file can be downloaded from here . 我的跟踪文件可以从这里下载。

When I plot only y axis in log scale. 当我仅以对数刻度绘制y轴时。 everything is fine 一切顺利

import pandas as pd
import numpy
import matplotlib.pyplot as plt

iplevel = pd.read_csv('iplevel.csv')
fig = plt.figure()
#plt.xscale('log')
plt.yscale('log')
plt.title(' Size Vs Duration (at IP level) for ')

plt.xlabel('Duration (in seconds)')
plt.ylabel('Size (in bytes)')
plt.scatter(iplevel['Time'], iplevel['Length'])
fig.tight_layout()
fig.savefig('iplevel_timevdur.png', dpi=fig.dpi)

对数刻度中只有y轴

When I plot both x and y axis in log scale, something strange happens 当我以对数比例绘制x和y轴时,会发生奇怪的情况

import pandas as pd
import numpy
import matplotlib.pyplot as plt

iplevel = pd.read_csv('iplevel.csv')
fig = plt.figure()
plt.xscale('log')
plt.yscale('log')
plt.title(' Size Vs Duration (at IP level) for ')

plt.xlabel('Duration (in seconds)')
plt.ylabel('Size (in bytes)')
plt.scatter(iplevel['Time'], iplevel['Length'])
fig.tight_layout()
fig.savefig('iplevel_timevdur.png', dpi=fig.dpi)

在此处输入图片说明

I am not sure where I am going wrong. 我不确定我要去哪里错。 Any ideas/suggestions welcome 欢迎任何想法/建议

It looks like you have some zeros in your X values. 看起来您的X值中有一些零。 log(0) isn't defined, log(veryclosetozero) is 10^{-verymuch} . 未定义log(0)log(veryclosetozero)10^{-verymuch}

Edit: 编辑:
In addition, float representation of numbers isn't always completely exact, so 0.0 might end up being stored as 0.00000000000000000001 or similar. 此外,数字的浮点表示并不总是完全准确,因此0.0可能最终会存储为0.00000000000000000001或类似的值。 The log function would not throw an error in that case, but simply calculate the logarithm of something very very small. 在这种情况下,对数函数不会引发错误,而只是计算非常小的东西的对数。

I faced a similar problem when plotting numbers containing a lot of zeros. 在绘制包含大量零的数字时,我遇到了类似的问题。 If your number is represented like 10E-38 format in the csv file, try multiplying all the rows by 1 and then read the data using pandas. 如果您的数字在csv文件中以10E-38格式表示,请尝试将所有行乘以1 ,然后使用熊猫读取数据。

This solved the problem in my case. 这解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM