简体   繁体   English

python matplotlib gridspec,不需要的任意轴标签

[英]python matplotlib gridspec, unwanted arbitrary axis labels

I have some code to plot a grid, with the data in each cell being distinct and having a very specific position. 我有一些代码来绘制网格,每个单元格中的数据是不同的,并且具有非常特定的位置。 The easiest way I found to do this was to create the grid with gridspec and use it to precisely position my subplots, however I'm having a problem where the overall grid is labelled from 0 to 1 along each axis. 我发现这样做最简单的方法是使用gridspec创建网格并使用它来精确定位我的子图,但是我遇到的问题是整个网格沿着每个轴标记为0到1。 This happens every time, even when the dimensions of the grid are changed. 每次都会发生这种情况,即使网格的尺寸发生变化也是如此。 Obviously these numbers have no relevance to my data, and as what I am aiming to display is qualitative rather than quantitative I would like to remove all labels from this plot entirely. 显然这些数字与我的数据无关,而且我想要显示的是定性而非定量,我想完全删除此图中的所有标签。

Here is a link to an image with an example of my problem 这是一个图像的链接,带有我的问题的一个例子

And here is the MWE that I used to create that image: 这是我用来创建该图像的MWE:

import numpy as np
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt

# mock-up of data being used
x = 6
y = 7
table = np.zeros((x, y))

# plotting
fig = plt.figure(1)
gs = gridspec.GridSpec(x, y, wspace=0, hspace=0)
plt.title('Example Plot')

for (j, k), img in np.ndenumerate(table):
   ax = fig.add_subplot(gs[x - j - 1, k])
   ax.set_xticklabels('')
   ax.set_yticklabels('')

plt.show()

I have not been able to find note of anything like this problem, so any help would be greatly appreciated. 我无法找到类似这个问题的任何记录,所以任何帮助都将非常感激。

If you just want to draw a grid over the plot, use this code: 如果您只想在绘图上绘制网格,请使用以下代码:

import numpy as np
import matplotlib.pyplot as plt

# mock-up of data being used
x = 6
y = 7
table = np.zeros((x, y))

# plotting
fig = plt.figure(1)
plt.title('Example Plot')

plt.gca().xaxis.grid(True, color='darkgrey', linestyle='-')
plt.gca().yaxis.grid(True, color='darkgrey', linestyle='-')
plt.show()

Another variant is used gridspec: 另一个变体是使用gridspec:

...
# hide ticks of main axes
ax0 = plt.gca()
ax0.get_xaxis().set_ticks([])
ax0.get_yaxis().set_ticks([])

gs = gridspec.GridSpec(x, y, wspace=0, hspace=0)
plt.title('Example Plot')

for (j, k), img in np.ndenumerate(table):
    ax = fig.add_subplot(gs[x - j - 1, k])
    # hide ticks of gribspec axes
    ax.get_xaxis().set_ticks([])
    ax.get_yaxis().set_ticks([])

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM