如何在 Matplotlib 中删除坐标轴?
How can axes be despined in Matplotlib?
我正在尝试使下面的绘图网格更清晰一些。我不希望左侧和底部的刻度线重叠。我试图通过尝试以下代码来取消坐标轴,但它似乎不起作用。有人有什么建议吗?
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")
在 运行 上面的代码之后,我得到了以下图表,但是,刻度线仍然重叠。
当您在未指定网格的情况下调用 plt.subplots()
时,它会在整个图中创建那些轴,其刻度线和标签会干扰最终图中的子图刻度标签。因此,将您的第一行代码更改为:
fig, ax = plt.subplots(2, 2, figsize=(15,10))
要么按照@Arne 的建议去做:
fig, ax = plt.subplots(rows, cols) #makes a grid of subplots
或者将前两行设为:
fig, ax = plt.subplots(figsize=(15,10))
ax.axis('off')
这将在添加其他子图之前移除整个子图周围的轴
我正在尝试使下面的绘图网格更清晰一些。我不希望左侧和底部的刻度线重叠。我试图通过尝试以下代码来取消坐标轴,但它似乎不起作用。有人有什么建议吗?
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")
在 运行 上面的代码之后,我得到了以下图表,但是,刻度线仍然重叠。
当您在未指定网格的情况下调用 plt.subplots()
时,它会在整个图中创建那些轴,其刻度线和标签会干扰最终图中的子图刻度标签。因此,将您的第一行代码更改为:
fig, ax = plt.subplots(2, 2, figsize=(15,10))
要么按照@Arne 的建议去做:
fig, ax = plt.subplots(rows, cols) #makes a grid of subplots
或者将前两行设为:
fig, ax = plt.subplots(figsize=(15,10))
ax.axis('off')
这将在添加其他子图之前移除整个子图周围的轴