Python 在 matplotlib 中将 x 轴移动到绘图的顶部
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14406214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Moving x-axis to the top of a plot in matplotlib
提问by Jason Sundram
Based on this question about heatmaps in matplotlib, I wanted to move the x-axis titles to the top of the plot.
基于这个关于 matplotlib 中热图的问题,我想将 x 轴标题移动到图的顶部。
import matplotlib.pyplot as plt
import numpy as np
column_labels = list('ABCD')
row_labels = list('WXYZ')
data = np.random.rand(4,4)
fig, ax = plt.subplots()
heatmap = ax.pcolor(data, cmap=plt.cm.Blues)
# put the major ticks at the middle of each cell
ax.set_xticks(np.arange(data.shape[0])+0.5, minor=False)
ax.set_yticks(np.arange(data.shape[1])+0.5, minor=False)
# want a more natural, table-like display
ax.invert_yaxis()
ax.xaxis.set_label_position('top') # <-- This doesn't work!
ax.set_xticklabels(row_labels, minor=False)
ax.set_yticklabels(column_labels, minor=False)
plt.show()
However, calling matplotlib's set_label_position(as notated above) doesn't seem to have the desired effect. Here's my output:
但是,调用matplotlib 的 set_label_position(如上所述)似乎并没有达到预期的效果。这是我的输出:


What am I doing wrong?
我究竟做错了什么?
采纳答案by unutbu
Use
用
ax.xaxis.tick_top()
to place the tick marks at the top of the image. The command
将刻度线放置在图像的顶部。命令
ax.set_xlabel('X LABEL')
ax.xaxis.set_label_position('top')
affects the label, not the tick marks.
影响标签,而不是刻度线。
import matplotlib.pyplot as plt
import numpy as np
column_labels = list('ABCD')
row_labels = list('WXYZ')
data = np.random.rand(4, 4)
fig, ax = plt.subplots()
heatmap = ax.pcolor(data, cmap=plt.cm.Blues)
# put the major ticks at the middle of each cell
ax.set_xticks(np.arange(data.shape[1]) + 0.5, minor=False)
ax.set_yticks(np.arange(data.shape[0]) + 0.5, minor=False)
# want a more natural, table-like display
ax.invert_yaxis()
ax.xaxis.tick_top()
ax.set_xticklabels(column_labels, minor=False)
ax.set_yticklabels(row_labels, minor=False)
plt.show()


回答by Lev Levitsky
You want set_ticks_positionrather than set_label_position:
你想要set_ticks_position而不是set_label_position:
ax.xaxis.set_ticks_position('top') # the rest is the same
This gives me:
这给了我:


回答by user1420304
You've got to do some extra massaging if you want the ticks (not labels) to show up on the top and bottom (not just the top). The only way I could do this is with a minor change to unutbu's code:
如果您希望刻度(不是标签)出现在顶部和底部(不仅仅是顶部),则必须进行一些额外的按摩。我能做到这一点的唯一方法是对 unutbu 的代码稍作改动:
import matplotlib.pyplot as plt
import numpy as np
column_labels = list('ABCD')
row_labels = list('WXYZ')
data = np.random.rand(4, 4)
fig, ax = plt.subplots()
heatmap = ax.pcolor(data, cmap=plt.cm.Blues)
# put the major ticks at the middle of each cell
ax.set_xticks(np.arange(data.shape[1]) + 0.5, minor=False)
ax.set_yticks(np.arange(data.shape[0]) + 0.5, minor=False)
# want a more natural, table-like display
ax.invert_yaxis()
ax.xaxis.tick_top()
ax.xaxis.set_ticks_position('both') # THIS IS THE ONLY CHANGE
ax.set_xticklabels(column_labels, minor=False)
ax.set_yticklabels(row_labels, minor=False)
plt.show()
Output:
输出:


回答by wSmit
tick_paramsis very useful for setting tick properties. Labels can be moved to the top with:
tick_params对于设置刻度属性非常有用。标签可以移动到顶部:
ax.tick_params(labelbottom=False,labeltop=True)

