3

I am trying to make use the polar plot projection to make a radar chart. I would like to know how to put only one grid line in bold (while the others should remain standard).

For my specific case, I would like to highlight the gridline associated to the 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()
DavidG
  • 19,281
  • 13
  • 70
  • 71

2 Answers2

2

The gridlines of a plot are Line2D objects. Therefore you can't make it bold. What you can do (as shown, in part, in the other answer) is to increase the linewidth and change the colour but rather than plot a new line you can do this to the specified gridline.

You first need to find the index of the y tick labels which you want to change:

y_tick_labels = [-100,-10,0,10]
ind = y_tick_labels.index(0) # find index of value 0

You can then get a list of the gridlines using gridlines = ax.yaxis.get_gridlines(). Then use the index you found previously on this list to change the properties of the correct gridline.

Using the example from the gallery as a basis, a full example is shown below:

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()

Which gives:

enter image description here

DavidG
  • 19,281
  • 13
  • 70
  • 71
0

It is just a trick, but I guess you could just plot a circle and change its linewidth and color to whatever could be bold for you. For example:

import matplotlib.pyplot as plt
import numpy as np

Yline = 0
Npoints = 300

angles = np.linspace(0,360,Npoints)*np.pi/180
line = 0*angles + Yline

ax = plt.subplot(111, projection='polar')
plt.plot(angles, line, color = 'k', linewidth = 3)
plt.ylim([-1,1])
plt.grid(True)
plt.show()

In this piece of code, I plot a line using plt.plot between any point of the two vectors angles and line. The former is actually all the angles between 0 and 2*np.pi. The latter is constant, and equal to the 'height' you want to plot that line Yline.

I suggest you try to decrease and increase Npoints while having a look to the documentaion of np.linspace() in order to understand your problem with the roundness of the circle.

Liris
  • 980
  • 7
  • 21
  • Thank you for this suggestion. I added the plt.plot line of your code and indeed it plotted a circle over the original plot. However, I don't understand how I can move this circle (if I wanted to; as of now it is plotted on the 0 coordinate of y which is nice) nor how I can bring a better resolution to this circle (it is not perfectly round-shaped as the other circles of the original plot...) Anyway, that's a significant advance thanks for that :) – Michaël Weber Dec 15 '18 at 15:21
  • I will edit my answer to take in account your questions. – Liris Dec 17 '18 at 09:42