python - 在图 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
推荐阅读
- python - 查找两个值之间的唯一数据
- java - Builder中的setService(java.lang.Class )不能应用于(java.lang.Class
) - javascript - 将上下箭头切换为图像
- javascript - IntelliJ在编写JSX时如何打开自动{}
- regex - 谷歌数据洞察中的 REGEXP_EXTRACT;继续“空”
- google-analytics - 网站上有多个 Google Analytics 代码
- php - PHP:mysqli插入数据很慢
- java - 在具有双向连接的特定实体上运行 findById(Long id) Jpa 方法时出现“java.lang.StackOverflowError: null”
- kdb - KDB 显示表,不是累积结果
- python - BeautifulSoup 不会提取单个标签