【问题标题】:Python: set x tick specifically for bars in subplot?Python:专门为子图中的条设置 x 刻度?
【发布时间】:2018-01-06 13:24:00
【问题描述】:

我想分析四个工具在运行多个程序时的性能。一个子图是一个工具在所有程序上的结果。结果应如下所示:

我使用 for 循环来迭代程序列表并每次绘制一个部分,如下所示:

但是这些图看起来像一个单独的图,我无法使用 axis.set_xticks() 分隔它们的 x 刻度。这个功能好像没有效果。

我是否使用正确的函数来设置 x 刻度?或者我应该如何制作这个情节?

draw_hist_query()可能是我的问题最重要的功能

数据样本:

boolector,ppbv,stp,z3
0.05349588394165039,0.015434503555297852,0.028127193450927734,0.11303281784057617
0.0027561187744140625,0.004331827163696289,0.007134914398193359,0.016040563583374023
0.003190755844116211,0.005587577819824219,0.002897500991821289,0.013916015625
0.009758472442626953,0.02006363868713379,0.0031282901763916016,0.011539697647094727
0.057138681411743164,0.012826681137084961,0.030836820602416992,0.0217435359954834

代码:

index = range(len(solvers))
fig, axes = plt.subplots(nrows=4)
solvers = ['z3', 'stp', 'boolector', 'ppbv']
colors = ['g', 'c', 'b', 'r', 'y', 'orange', 'grey']
ticks = [0.1, 0.5, 1.0, 2.0]
width=0.2

# program entry
def all_time_query(path):
    csv = xxx.csv # the array of data to be analyzed, one csv for one program
    for axis in axes:
        axis.set_xticks(range(len(csv)))
    for c in csv:
        multi_time_query(c) # draw the bar pair for c, which shows the upper image for one program on four tools


def multi_time_query(csv):
    data = pd.read_csv(csv)
    for solver in solvers: # the four tools
        bin = index[solvers.index(solver)]
        hist_t_query(data, solver, ax=axes[bin]) # details to draw the bar pair, uses dataframe.plot.bar



def hist_t_query(data, solver, ax):
    solver_data = pd.DataFrame(data).as_matrix(columns=[solver])
    # draw one bar for demo
    draw_hist_query(pd.DataFrame(solver_data), ax)


# left of bar pair, the right one is similar
def draw_hist_query(df, ax):
    count = []
    for i in range(len(ticks)):
        count.append(df[df < ticks[i]].count())
        color = stat.colors[i]
        if i == 0:
            count[i].plot.bar(ax=ax, color=color, width=width, position=0)
        else:
            (count[i] - count[i - 1]).plot.bar(bottom=count[i - 1],
                                               ax=ax, color=color, width=width, position=0)

【问题讨论】:

  • 你能添加一些代码吗?你使用子图吗?如果是这样,那么您可以使用 subplots_adjust matplotlib.org/api/… 调整它们
  • @KacperWolkowski 添加了一些代码
  • 我认为你也需要显示hist_t_query(尤其是你调用dataframe.plot.bar的部分),否则很难知道发生了什么。
  • 让我只提以下几点:如果您使用代码中硬编码的一些数据创建问题的minimal reproducible example,以便人们能够重现问题,那么您的问题很可能会出现SO在几个小时内解决了。如果您不提供这样的minimal reproducible example,此对话可能会一直持续下去,您可能会收到与您的问题无关的任意答案,如下所示。
  • 它可能是唯一负责该问题的功能,是的。因此,我们的想法是在代码中创建一个数据框,以便代码可运行并重现问题,这反过来又可以让人们为您提出解决方案。

标签: python pandas matplotlib visualization


【解决方案1】:

一般来说,您几乎没有选择。您可以使用plt.tight_layout(),它会自动执行所有操作,或者您可以使用plt.subplot_adjust() 并自己指定每个参数。 正如您在文档中看到的,签名是这样的:

subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)

如果您将进入交互式窗口,您可以选择在那里调整参数,您可以看到图表将如何随每个参数而变化

然后你可以用最适合你的方法调用 subplot_adjust。

希望对你有帮助。

【讨论】:

  • 其实我的问题出在subplots上,我自己解决了。如果你有兴趣,请看我的回答。还是一样的谢谢你:)
【解决方案2】:

最后我解决了这个问题:

我之前的错误想法是关于次要情节。在一对已经存在之后,我想在子图中添加另一对条形。 但是,一个子图应该一起绘制(一次),并且不应该分开。在我的例子中,一个子图的条应该一起出现,只需要四次绘制所有子图.

这是我的新版本代码:

def time_query_project(path):
    fig, axis = plt.subplots(nrows=4)
    csv = sio.find_csv(path)
    data = {}

    for solver in solvers:
        for c in csv:
            df = pd.DataFrame(pd.read_csv(c), columns=[solver])
            data.update({get_name(c): df.to_dict()[solver]})
        df = pd.DataFrame.from_dict(data, orient='columns')
        ax = axis[solvers.index(solver)]
        ax.set_ylabel(solver)
        hist_t_query(df, ax)


def hist_t_query(data, solver, ax):
    solver_data = pd.DataFrame(data).as_matrix(columns=[solver])
    # draw one bar for demo
    draw_hist_query(pd.DataFrame(solver_data), ax)


# left of bar pair, the right one is similar
def draw_hist_query(df, ax):
    count = []
    for i in range(len(ticks)):
        count.append(df[df < ticks[i]].count())
        color = stat.colors[i]
        if i == 0:
            count[i].plot.bar(ax=ax, color=color, width=width, position=0)
        else:
            (count[i] - count[i - 1]).plot.bar(bottom=count[i - 1],
                                               ax=ax, color=color, width=width, position=0)

【讨论】:

    猜你喜欢
    • 2021-04-08
    • 2022-06-10
    • 2019-07-03
    • 1970-01-01
    • 2020-05-29
    • 2015-02-13
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多