我正在尝试使用极坐标图投影来制作雷达图。我想知道如何只用粗体显示一条网格线(而其他的应保持标准)。
对于我的具体情况,我想突出显示与 ytick“0”关联的网格线。
from matplotlib import pyplot as plt
import pandas as pd
import numpy as np
#Variables
sespi = pd.read_csv("country_progress.csv")
labels = sespi.country
progress = sespi.progress
angles=np.linspace(0, 2*np.pi, len(labels), endpoint=False)
#Concatenation to close the plots
progress=np.concatenate((progress,[progress[0]]))
angles=np.concatenate((angles,[angles[0]]))
#Polar plot
fig=plt.figure()
ax = fig.add_subplot(111, polar=True)
ax.plot(angles, progress, '.--', linewidth=1, c="g")
#ax.fill(angles, progress, alpha=0.25)
ax.set_thetagrids(angles * 180/np.pi, labels)
ax.set_yticklabels([-200,-150,-100,-50,0,50,100,150,200])
#ax.set_title()
ax.grid(True)
plt.show()
请您参考如下方法:
绘图的网格线是 Line2D 对象。因此,您不能将其设为粗体。您可以做的(如另一个答案部分所示)是增加线宽并更改颜色,而不是绘制一条新线,您可以对指定的网格线执行此操作。
您首先需要找到要更改的 y 刻度标签的索引:
y_tick_labels = [-100,-10,0,10]
ind = y_tick_labels.index(0) # find index of value 0
然后您可以使用 gridlines = ax.yaxis.get_gridlines() 获取网格线列表。然后使用您之前在此列表中找到的索引更改正确网格线的属性。
使用 gallery 中的示例作为基础,下面显示了一个完整的示例:
r = np.arange(0, 2, 0.01)
theta = 2 * np.pi * r
ax = plt.subplot(111, projection='polar')
ax.set_rmax(2)
ax.set_rticks([0.5, 1, 1.5, 2]) # less radial ticks
ax.set_rlabel_position(-22.5) # get radial labels away from plotted line
ax.grid(True)
y_tick_labels = [-100, -10, 0, 10]
ax.set_yticklabels(y_tick_labels)
ind = y_tick_labels.index(0) # find index of value 0
gridlines = ax.yaxis.get_gridlines()
gridlines[ind].set_color("k")
gridlines[ind].set_linewidth(2.5)
plt.show()
给出:
