python - 如何删除特定的网格线?

标签 python matplotlib gridlines

请参阅下面的结果图。

我希望仅删除 y 轴值为 10 的一条主要网格线(蓝色水平线),并保留所有其他网格线。

有办法做到这一点吗?

enter image description here

    plt.rcParams['font.family'] = 'Arial'
    fig, ax = plt.subplots(figsize=(14.78, 9.84))
    plt.xlim(0, 105)
    plt.ylim(0, 10)
    ax.xaxis.set_minor_locator(AutoMinorLocator(2))
    ax.yaxis.set_minor_locator(AutoMinorLocator(2))
    ax.spines['bottom'].set_linewidth(1.5)
    ax.spines['left'].set_linewidth(1.5)
    ax.spines['top'].set_linewidth(0)
    ax.spines['right'].set_linewidth(0)
    # Grid setting
    plt.grid(True, color='#0100FF', which="major", ls="-")
    plt.grid(True, color='#0BC904', which="minor", ls="-")
    plt.xlabel("Indicator Amplitude, %FSH", fontsize=28, labelpad=15)
    plt.ylabel("Function Generator Output, V", fontsize=28, labelpad=15)
    # Axis setting
    plt.tick_params(which="major", labelsize=22, length=10, pad=10, width=1.5)
    plt.tick_params(which="minor", length=8, width=1.5)
    # Plot scatter & line
    plt.plot(FSH_axis, x_value[2:], color='black', marker='^', linewidth=1.5, markersize=8, label="40 dB")
    plt.plot(FSH_axis, y_value[2:], color='red', marker='o', linewidth=1.5, markersize=8, label="60 dB")
    plt.plot(FSH_axis, z_value[2:], color='blue', marker='v', linewidth=1.5, markersize=8, label="80 dB")

    plt.legend(loc=(1 / 16, 58 / 90), ncol=1, fontsize=20, frameon=True, framealpha=1, edgecolor="black")
    plt.show()

最佳答案

我们可以使用 get_ygridlines() 捕获所有网格线,然后将各个网格线作为 Line2D 对象访问以对其进行修改:

from matplotlib import pyplot as plt
from matplotlib.ticker import AutoMinorLocator

plt.rcParams['font.family'] = 'Arial'
fig, ax = plt.subplots(figsize=(14.78, 9.84))
plt.xlim(0, 105)
plt.ylim(0, 10)
ax.xaxis.set_minor_locator(AutoMinorLocator(2))
ax.yaxis.set_minor_locator(AutoMinorLocator(2))

ax.spines['bottom'].set_linewidth(1.5)
ax.spines['left'].set_linewidth(1.5)
ax.spines['top'].set_linewidth(0)
ax.spines['right'].set_linewidth(0)
# Grid setting
plt.grid(True, color='#0100FF', which="major", ls="-")
plt.grid(True, color='#0BC904', which="minor", ls="-")

#this part is added
#set the last horizontal gridline invisible 
ygridlines = ax.get_ygridlines()
gridline_of_interest = ygridlines[-1]
gridline_of_interest.set_visible(False)

plt.xlabel("Indicator Amplitude, %FSH", fontsize=28, labelpad=15)
plt.ylabel("Function Generator Output, V", fontsize=28, labelpad=15)
# Axis setting
plt.tick_params(which="major", labelsize=22, length=10, pad=10, width=1.5)
plt.tick_params(which="minor", length=8, width=1.5)
# Plot scatter & line
FSH_axis = [10, 40, 100]

plt.plot(FSH_axis, [1, 3, 2], color='black', marker='^', linewidth=1.5, markersize=8, label="40 dB")
plt.plot(FSH_axis, [2, 2, 3], color='red', marker='o', linewidth=1.5, markersize=8, label="60 dB")
plt.plot(FSH_axis, [2, 1, 1], color='blue', marker='v', linewidth=1.5, markersize=8, label="80 dB")

plt.legend(loc=(1 / 16, 58 / 90), ncol=1, fontsize=20, frameon=True, framealpha=1, edgecolor="black")
plt.show()

示例输出: enter image description here

当然是对应get_xgridlines()也存在。

关于python - 如何删除特定的网格线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70588196/

相关文章:

python - 一次制作多个地 block

python - Beautifulsoup 使用 findall() 不返回任何内容

python - 如何在 Matplotlib 中生成嵌套图例

python - 如何根据数据框中的值绘制带有颜色的表格?

python - ndimage.find_object ...颜色特征之后如何?

python - 如何将 Matplotlib.pyplot 中的绘图插入到 FPDF 文档中?

Chart.js如何显示刻度线但隐藏网格线

r - R 中有吸引力的 3D 绘图

wpf - 如何让网格为空白单元格绘制边框?

python - 打印出可选的正则表达式