首页 > 解决方案 > 在图 yticks-xticks 上排列值和标签

问题描述

在此处输入图像描述

height_str=headers[1:]   #height format str
height=[float(i) for i in height_str]  #height format float -type:LIST

plt.yticks((height),height_str)  #height  y axis

你好。我想在绘图的更好范围内显示值和标签。我使用 imshow 作为图表。height 是一个包含 156 个元素的列表

标签: pythonmatplotlibimshowxticks

解决方案


您提供刻度的方式是告诉 matplotlib 在您给它的每个高度值上放置一个刻度。如果你以这种方式做事,你只需要给它你想要使用的特定刻度。

import numpy as np
import matplotlib.pyplot as plt
from scipy.misc import face

# Generating my face heights
height = np.arange(0, 800)
height_str = list(map(str, height))
plt.imshow(face())
#plt.yticks(height,height_str)  # <- this is bad. Do this instead
plt.yticks(height[::50],height_str[::50]) # Gets every 50th entry

否则,您可以这样做:

fig, ax = plt.subplots()
ax.imshow(face())
ax.set_yticks(height[::50])

编辑:要格式化,您可以使用自动收录器功能。

from matplotlib import ticker
ax.yaxis.set_major_formatter(ticker.StrMethodFormatter("{x:.2f}"))

来自https://matplotlib.org/stable/gallery/ticks_and_spines/tick-formatters.html


推荐阅读