import matplotlib.pyplot as pltimport random
prefix =6.18
rx =[prefix+(0.001*random.random())for i in arange(100)]
ry =[prefix+(0.001*random.random())for i in arange(100)]
plt.plot(rx,ry,'ko')
frame1 = plt.gca()for xlabel_i in frame1.axes.get_xticklabels():
xlabel_i.set_visible(False)
xlabel_i.set_fontsize(0.0)for xlabel_i in frame1.axes.get_yticklabels():
xlabel_i.set_fontsize(0.0)
xlabel_i.set_visible(False)for tick in frame1.axes.get_xticklines():
tick.set_visible(False)for tick in frame1.axes.get_yticklines():
tick.set_visible(False)
plt.show()
I’m trying to plot a figure without tickmarks or numbers on either of the axes (I use axes in the traditional sense, not the matplotlib nomenclature!). An issue I have come across is where matplotlib adjusts the x(y)ticklabels by subtracting a value N, then adds N at the end of the axis.
This may be vague, but the following simplified example highlights the issue, with ‘6.18’ being the offending value of N:
import matplotlib.pyplot as plt
import random
prefix = 6.18
rx = [prefix+(0.001*random.random()) for i in arange(100)]
ry = [prefix+(0.001*random.random()) for i in arange(100)]
plt.plot(rx,ry,'ko')
frame1 = plt.gca()
for xlabel_i in frame1.axes.get_xticklabels():
xlabel_i.set_visible(False)
xlabel_i.set_fontsize(0.0)
for xlabel_i in frame1.axes.get_yticklabels():
xlabel_i.set_fontsize(0.0)
xlabel_i.set_visible(False)
for tick in frame1.axes.get_xticklines():
tick.set_visible(False)
for tick in frame1.axes.get_yticklines():
tick.set_visible(False)
plt.show()
The three things I would like to know are:
How to turn off this behaviour in the first place (although in most cases it is useful, it is not always!) I have looked through matplotlib.axis.XAxis and cannot find anything appropriate
How can I make N disappear (i.e. X.set_visible(False))
Is there a better way to do the above anyway? My final plot would be 4×4 subplots in a figure, if that is relevant.
I was not actually able to render an image without borders or axis data based on any of the code snippets here (even the one accepted at the answer). After digging through some API documentation, I landed on this code to render my image
I used the tick_params call to basically shut down any extra information that might be rendered and I have a perfect graph in my output file.
回答 5
我已经对该图进行了颜色编码以简化此过程。
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
您可以使用以下命令完全控制图形,以完成答案,我还添加了对样条线的控制:
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)# X AXIS -BORDER
ax.spines['bottom'].set_visible(False)# BLUE
ax.set_xticklabels([])# RED
ax.set_xticks([])# RED AND BLUE TOGETHER
ax.axes.get_xaxis().set_visible(False)# Y AXIS -BORDER
ax.spines['left'].set_visible(False)# YELLOW
ax.set_yticklabels([])# GREEN
ax.set_yticks([])# YELLOW AND GREEN TOGHETHER
ax.axes.get_yaxis().set_visible(False)
I’ve colour coded this figure to ease the process.
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
You can have full control over the figure using these commands, to complete the answer I’ve add also the control over the splines:
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
# X AXIS -BORDER
ax.spines['bottom'].set_visible(False)
# BLUE
ax.set_xticklabels([])
# RED
ax.set_xticks([])
# RED AND BLUE TOGETHER
ax.axes.get_xaxis().set_visible(False)
# Y AXIS -BORDER
ax.spines['left'].set_visible(False)
# YELLOW
ax.set_yticklabels([])
# GREEN
ax.set_yticks([])
# YELLOW AND GREEN TOGHETHER
ax.axes.get_yaxis().set_visible(False)
When using the object oriented API, the Axes object has two useful methods for removing the axis text, set_xticklabels() and set_xticks().
Say you create a plot using
fig, ax = plt.subplots(1)
ax.plot(x, y)
If you simply want to remove the tick labels, you could use
ax.set_xticklabels([])
or to remove the ticks completely, you could use
ax.set_xticks([])
These methods are useful for specifying exactly where you want the ticks and how you want them labeled. Passing an empty list results in no ticks, or no labels, respectively.