【发布时间】:2021-10-15 21:05:37
【问题描述】:
下面的代码产生这样的图:
我只需要在 y 轴上显示在水平线上 的刻度标签。在这种情况下,标签[2,3,4,5] 需要隐藏。我试过使用
ax.get_yticks()
ax.get_yticklabels()
检索已绘制的刻度,并从中仅选择y_min 值以上的刻度来显示。这两个命令都不会返回绘图中绘制的实际刻度标签。
我该怎么做?
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter
# Some random data
x = np.random.uniform(1, 20, 100)
y = np.array(list(np.random.uniform(1, 150, 97)) + [4, 7, 9])
y_min = np.random.uniform(4, 10)
ax = plt.subplot(111)
ax.scatter(x, y)
ax.hlines(y_min, xmin=min(x), xmax=max(x))
ax.set_xscale('log')
ax.set_yscale('log')
ax.yaxis.set_minor_formatter(FormatStrFormatter('%.0f'))
ax.yaxis.set_major_formatter(FormatStrFormatter('%.0f'))
plt.show()
【问题讨论】:
-
试试
ax.set_ylim(y_min,y_max) -
@ShervinRad - 这是一个 NameError: name 'y_max' is not defined,OP 询问的是 yticklabels,而不是 ylim。
标签: python numpy matplotlib plot data-visualization