【问题标题】:How to change y-label step in pandas boxplot如何更改熊猫箱线图中的y标签步骤
【发布时间】: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


【解决方案1】:

使用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()  

【讨论】:

  • 是的,正是我要找的东西,虽然我没想到它可以以这种方式编辑,并且想知道它到底如何知道如何将任意标签与实际时间值对齐。如果我使用一系列字母会怎样?
  • 不确定我的理解是否正确,但类似这样?imgur.com/k7ndIAJ
猜你喜欢
  • 2017-01-10
  • 2015-05-04
  • 2018-02-23
  • 2020-04-17
  • 2021-07-11
  • 2017-02-28
  • 2013-10-23
  • 2018-11-30
  • 2013-05-21
相关资源
最近更新 更多