python - 使用 set_yticklabels 时如何反转 (invert_yaxis())
问题描述
我正在尝试使用 .invert_yaxis(),但使用 set_yticklabels 时会出现错误消息。
无论如何在下面的代码上使用反转?
import matplotlib.pyplot as plt
import numpy as np
x = [1.0, 1.1, 2.0, 5.7]
y = np.arange(len(x))
fsize=(2,2)
fig, ax = plt.subplots(1,1,figsize=fsize)
ax.set_yticklabels(list(' abcd')) #if the abcd is data which get from upper code
ax.barh(y,x,align='center',color='grey')
plt.show()
解决方案
使用set_yticklabels
时强烈建议先设置set_yticks
。一般来说,matplotlib 在内部决定将刻度放在哪里。代码中的微小更改可能会将刻度线放在不同的位置。通过明确使用set_yticks
,您可以确定他们的位置。
import matplotlib.pyplot as plt
import numpy as np
x = [1.0, 1.1, 2.0, 5.7]
y = np.arange(len(x))
fsize=(2,2)
fig, ax = plt.subplots(1,1,figsize=fsize)
ax.barh(y,x,align='center',color='grey')
ax.set_yticks(y)
ax.set_yticklabels(list('abcd')) #if the abcd is data which get from upper code
ax.invert_yaxis()
plt.show()
推荐阅读
- c# - C# Selenium CSS-selector 属性不起作用
- mysql - 如何处理查询字符串 Node Js AWS Lambda
- django - Django:限制员工用户向组授予比他自己更多的权限
- c# - 有什么方法可以使用 NPOI 在我的 excel 文档中从像“#72fe9c”这样的十六进制值向单元格添加颜色
- java - Spring MultipartFile 参数不尊重配置的 maxFileSize
- amazon-web-services - AWS 静态 IP 到负载均衡器?
- powershell - 添加压缩文件名的路径
- javascript - 如何在一个元素的多个类中附加到一个特定的类?
- api - 连接到 Aurora 的内部服务器错误
- java - 显示 10% 并在 String.xml 中有一个 %s