Jas*_*ram 101 python plot data-visualization matplotlib
基于这个关于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()
Run Code Online (Sandbox Code Playgroud)
但是,调用matplotlib的set_label_position(如上所述)似乎没有达到预期的效果.这是我的输出:
我究竟做错了什么?
unu*_*tbu 130
使用
ax.xaxis.tick_top()
Run Code Online (Sandbox Code Playgroud)
将刻度线放在图像的顶部.命令
ax.set_xlabel('X LABEL')
ax.xaxis.set_label_position('top')
Run Code Online (Sandbox Code Playgroud)
影响标签,而不是刻度线.
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()
Run Code Online (Sandbox Code Playgroud)
Lev*_*sky 30
你想要set_ticks_position
而不是set_label_position
:
ax.xaxis.set_ticks_position('top') # the rest is the same
Run Code Online (Sandbox Code Playgroud)
这给了我:
wSm*_*mit 15
tick_params对于设置刻度属性非常有用.标签可以移动到顶部:
ax.tick_params(labelbottom='off',labeltop='on')
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
68945 次 |
最近记录: |