如何更改 pandas 箱线图中的 y 标签步骤

How to change y-label step in pandas boxplot

我只想更改 y 轴上的标签以显示更多数字。例如,范围为 0 - 40,它显示数字 0、10、20、30、40。 我想看到 0、1、2、3、4、... 38、39、40。 我还想要显示一个网格(支持线或它的调用方式)。

我的代码看起来像这样,其中我有一个包含训练数据集名称、分类器名称和时间的数据框。 我正在为每个分类器创建一个箱线图,显示该分类器在所有数据集上花费的时间。

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


## agg backend is used to create plot as a .png file
mpl.use('agg')

# read dataset
data = pd.read_csv("classifier_times_sml.csv", ";")
# extract data
g = data.sort_values("time", ascending=False)[["classifier", "train", "time"]].groupby("classifier")

# Create a figure instance
fig = plt.figure(1, figsize=(20, 30))
# Create an axes instance
ax = fig.add_subplot(111)


labels = []
times = []
counter = 0
for group, group_df in g:
    # Create the boxplot

    times.append( np.asarray(group_df["time"]) )
    labels.append(group)

# Create the boxplot
bp = ax.boxplot(times, showfliers=False )
ax.set_xticklabels(labels, rotation=90)


# Save the figure
fig.savefig('times_sml.png', bbox_inches='tight')

我一直在彻底搜索,但没有找到任何有用的箱线图选项。此处不允许 ax.boxplot(...) 的网格选项。我做错了什么?

使用ax.set_yticks(np.arange(min,max,step))plt.yticks(np.arange(min,max,step))
ax.grid(True) 打开网格。

您正在寻找这样的东西吗?

import pandas as pd, numpy as np
import matplotlib.pyplot as plt
import seaborn as sns;sns.set()

from numpy import arange

data = np.random.randint(0,40,size=40)
fig = plt.figure(1, figsize=(20, 30))
ax = fig.add_subplot(111)
ax.boxplot(data)  

ax.set_yticks(np.arange(0, 40, 1.0))
ax.grid(True)
plt.show()