【问题标题】:How can axes be despined in Matplotlib?如何在 Matplotlib 中指定轴?
【发布时间】:2020-03-29 18:00:45
【问题描述】:

我正在尝试使下面的绘图网格更清晰一些。我不希望左侧和底部的刻度线重叠。我试图通过尝试下面的代码来定义轴,但它似乎不起作用。有人有什么建议吗?

fig, ax = plt.subplots(figsize=(15,10))
cols = ['x6', 'x7', 'x16', 'x17']
subset = df[cols]
normed_df = (subset-subset.min())/(subset.max()-subset.min())
style.use('seaborn-darkgrid')
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)
for sp in range(4):
    ax = fig.add_subplot(2,2, sp+1)
    ax.hist(normed_df[cols[sp]], density=True)
    normed_df[cols[sp]].plot.kde(ax=ax)
ax.tick_params(bottom="off", top="off", left="off", right="off")

运行上面的代码后,我得到了以下图,但是,刻度仍然重叠。

【问题讨论】:

    标签: python matplotlib seaborn visualization


    【解决方案1】:

    要么按照@Arne 的建议去做:

    fig, ax = plt.subplots(rows, cols) #makes a grid of subplots
    

    或者把你的前两行写成这样:

    fig, ax = plt.subplots(figsize=(15,10))
    ax.axis('off')
    

    这将在添加其他子图之前删除整个子图周围的轴

    【讨论】:

      【解决方案2】:

      当您在未指定网格的情况下调用 plt.subplots() 时,它会在整个图形中创建那些轴,其刻度线和标签会干扰最终图中的子图刻度线标签。所以把你的第一行代码改成这样:

      fig, ax = plt.subplots(2, 2, figsize=(15,10))
      

      【讨论】:

        猜你喜欢
        • 2014-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-26
        • 2019-12-10
        • 2020-04-21
        • 2014-03-31
        相关资源
        最近更新 更多