你可以使用logx和logy参数在pandas中分别在x轴和y轴上创建具有对数刻度的柱状图:
#create histogram with log scale on x-axis
df['my_column'].plot(kind='hist', logx=True)
#create histogram with log scale on y-axis
df['my_column'].plot(kind='hist', logy=True)
下面的例子展示了如何使用这些参数在pandas中创建具有对数刻度的直方图。
相关的: 什么时候应该在图表中使用对数刻度?
例子:在Pandas中创建具有对数刻度的直方图
假设我们有下面这个有5000行的pandas DataFrame:
import pandas as pd
import numpy as np
#make this example reproducible
np.random.seed(1)
#create DataFrame
df = pd.DataFrame({'values': np.random.lognormal(size=5000)})
#view first five rows of DataFrame
print(df.head())
values
0 5.075096
1 0.542397
2 0.589682
3 0.341992
4 2.375974
我们可以使用下面的语法来创建一个在x轴和y轴上都有线性刻度的直方图:
#create histogram
df['values'].plot(kind='hist')

目前x轴和y轴都有一个线性刻度。
我们可以使用logx=True参数将x轴转换为对数刻度。
#create histogram with log scale on x-axis
df['values'].plot(kind='hist', logx=True)

现在x轴上的数值遵循对数刻度。
我们还可以使用logy=True参数将y轴转换为对数刻度。
#create histogram with log scale on y-axis
df['values'].plot(kind='hist', logy=True)

y轴上的数值现在遵循对数刻度。