想要将 Pandas 数据框绘制为具有 log10 刻度 x 轴的多个直方图
Want to plot Pandas Dataframe as Multiple Histograms with log10 scale x-axis
我在 Pandas 数据帧中有浮点数据。每列代表一个变量(它们有字符串名称),每一行代表一组值(这些行有不重要的整数名称)。
>>> print data
0 kppawr23 kppaspyd
1 3.312387 13.266040
2 2.775202 0.100000
3 100.000000 100.000000
4 100.000000 39.437420
5 17.017150 33.019040
...
我想为每一列绘制直方图。我取得的最好结果是使用 dataframe 的 hist 方法:
data.hist(bins=20)
但我希望每个直方图的 x 轴都在 log10 范围内。并且垃圾箱也要在 log10 规模上,但这很容易 bins=np.logspace(-2,2,20).
解决方法可能是在绘图之前对数据进行 log10 转换,但我已经尝试过的方法,
data.apply(math.log10)
和
data.apply(lambda x: math.log10(x))
给我一个浮点错误。
"cannot convert the series to {0}".format(str(converter)))
TypeError: ("cannot convert the series to <type 'float'>", u'occurred at index kppawr23')
你可以使用
ax.set_xscale('log')
data.hist()
returns 轴数组。你需要打电话
ax.set_xscale('log')
为每个轴,ax
使每个对数
缩放。
例如,
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
np.random.seed(2015)
N = 100
arr = np.random.random((N,2)) * np.logspace(-2,2,N)[:, np.newaxis]
data = pd.DataFrame(arr, columns=['kppawr23', 'kppaspyd'])
bins = np.logspace(-2,2,20)
axs = data.hist(bins=bins)
for ax in axs.ravel():
ax.set_xscale('log')
plt.gcf().tight_layout()
plt.show()
产量
顺便说一下,要获取 DataFrame 中每个值的日志,data
,您可以使用
logdata = np.log10(data)
因为 NumPy ufunc(例如 np.log10
)可以应用于 pandas DataFrames 因为它们操作 elementwise on all the values in the DataFrame.
data.apply(math.log10)
无效,因为 apply
试图将整列(一系列)值传递给 math.log10
。 math.log10
只需要一个标量值。
data.apply(lambda x: math.log10(x))
失败的原因与 data.apply(math.log10)
相同。此外,如果 data.apply(func)
和 data.apply(lambda x: func(x))
都是可行的选项,则应该首选第一个,因为 lambda 函数只会使调用速度稍慢。
您可以再次使用 data.apply(np.log10)
,因为 NumPy ufunc np.log10
可以应用于系列,但是当 np.log10(data)
工作时没有理由费心这样做。
您也可以使用 data.applymap(math.log10)
因为 applymap
调用
math.log10
对 data
中的每个值一次一个。但这会慢得多
比调用等效的 NumPy 函数 np.log10
数据框。尽管如此,还是值得了解 applymap
以防您需要致电
一些不是 ufunc 的自定义函数。
我在 Pandas 数据帧中有浮点数据。每列代表一个变量(它们有字符串名称),每一行代表一组值(这些行有不重要的整数名称)。
>>> print data
0 kppawr23 kppaspyd
1 3.312387 13.266040
2 2.775202 0.100000
3 100.000000 100.000000
4 100.000000 39.437420
5 17.017150 33.019040
...
我想为每一列绘制直方图。我取得的最好结果是使用 dataframe 的 hist 方法:
data.hist(bins=20)
但我希望每个直方图的 x 轴都在 log10 范围内。并且垃圾箱也要在 log10 规模上,但这很容易 bins=np.logspace(-2,2,20).
解决方法可能是在绘图之前对数据进行 log10 转换,但我已经尝试过的方法,
data.apply(math.log10)
和
data.apply(lambda x: math.log10(x))
给我一个浮点错误。
"cannot convert the series to {0}".format(str(converter)))
TypeError: ("cannot convert the series to <type 'float'>", u'occurred at index kppawr23')
你可以使用
ax.set_xscale('log')
data.hist()
returns 轴数组。你需要打电话
ax.set_xscale('log')
为每个轴,ax
使每个对数
缩放。
例如,
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
np.random.seed(2015)
N = 100
arr = np.random.random((N,2)) * np.logspace(-2,2,N)[:, np.newaxis]
data = pd.DataFrame(arr, columns=['kppawr23', 'kppaspyd'])
bins = np.logspace(-2,2,20)
axs = data.hist(bins=bins)
for ax in axs.ravel():
ax.set_xscale('log')
plt.gcf().tight_layout()
plt.show()
产量
顺便说一下,要获取 DataFrame 中每个值的日志,data
,您可以使用
logdata = np.log10(data)
因为 NumPy ufunc(例如 np.log10
)可以应用于 pandas DataFrames 因为它们操作 elementwise on all the values in the DataFrame.
data.apply(math.log10)
无效,因为 apply
试图将整列(一系列)值传递给 math.log10
。 math.log10
只需要一个标量值。
data.apply(lambda x: math.log10(x))
失败的原因与 data.apply(math.log10)
相同。此外,如果 data.apply(func)
和 data.apply(lambda x: func(x))
都是可行的选项,则应该首选第一个,因为 lambda 函数只会使调用速度稍慢。
您可以再次使用 data.apply(np.log10)
,因为 NumPy ufunc np.log10
可以应用于系列,但是当 np.log10(data)
工作时没有理由费心这样做。
您也可以使用 data.applymap(math.log10)
因为 applymap
调用
math.log10
对 data
中的每个值一次一个。但这会慢得多
比调用等效的 NumPy 函数 np.log10
数据框。尽管如此,还是值得了解 applymap
以防您需要致电
一些不是 ufunc 的自定义函数。