【发布时间】:2020-03-13 16:15:28
【问题描述】:
我只是想更改 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.grid(True)会报错? -
不,我实际上是在尝试调用 ax.boxplot( ... , grid=True)
标签: python pandas matplotlib boxplot