在图上排列值和标签 yticks-xticks
arrange values and labels on plots 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 个元素的列表
您提供刻度的方式是告诉 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
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 个元素的列表
您提供刻度的方式是告诉 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