By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,469 Members | 1,050 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,469 IT Pros & Developers. It's quick & easy.

Adding color to radar chart in python

P: 5
Hello,

What code do I use to change the color background of a radar chart using python.
I want to use a sequential color scheme from light blue to dark blue.
I assume that the starting point would be from matplotlib.pyplot using the cmaps.

Any help is appreciated.
3 Weeks Ago #1
Share this Question
Share on Google+
8 Replies


100+
P: 110
col indicates an RGB color between 0.0 and 1.0.
It can change the background color by adjusting RGB.
Expand|Select|Wrap|Line Numbers
  1.     fig = plt.figure()
  2. :
  3. :
  4. :
  5.     col = [0.0, 0.0, 1.0]
  6.     fig.savefig(imgname, facecolor=col)
  7.  
3 Weeks Ago #2

P: 5
Thanks for the help.

How do I code the 'Blues' from the sequential colormap as found on this page: https://matplotlib.org/tutorials/colors/colormaps.html
3 Weeks Ago #3

100+
P: 110
Hexadecimal or RGB specified, change the blue color as shown below.
'#0000ff' is the same as [0.0, 0.0, 1.0].
Expand|Select|Wrap|Line Numbers
  1. fig = plt.figure()
  2. col1 = [0.3, 0.2, 0.5]
  3. col2 = [0.2, 0.3, 0.5]
  4. col3 = [0.1, 0.3, 0.5]
  5. col4 = [0.0, 0.3, 0.6]
  6. col5 = [0.0, 0.3, 0.7]
  7. col6 = [0.0, 0.2, 0.8]
  8. col7 = [0.0, 0.1, 0.9]
  9. colorlist = [col1, col2, col3, col4, col5, col6, col7, '#0000ff']
  10. x = np.arange(1, 9)
  11. height = np.arange(1, 9)
  12. plt.barh(x, height, color=colorlist, tick_label=colorlist, align="center")
  13. fig.savefig("test1.png")
3 Weeks Ago #4

P: 5
Where would I code that into this example. I want to change the background of the radar chart with the "Blues' Sequential colormap.
Expand|Select|Wrap|Line Numbers
  1. # Plots a radar chart.
  2.  
  3. from math import pi
  4. import matplotlib.pyplot as plt
  5.  
  6.  
  7. # Set data
  8. cat = ['Speed', 'Reliability', 'Comfort', 'Safety', 'Effieciency']
  9. values = [90, 60, 65, 70, 40]
  10.  
  11. N = len(cat)
  12.  
  13. x_as = [n / float(N) * 2 * pi for n in range(N)]
  14.  
  15. # Because our chart will be circular we need to append a copy of the first 
  16. # value of each list at the end of each list with data
  17. values += values[:1]
  18. x_as += x_as[:1]
  19.  
  20.  
  21. # Set color of axes
  22. plt.rc('axes', linewidth=0.5, edgecolor="#888888")
  23.  
  24.  
  25. # Create polar plot
  26. ax = plt.subplot(111, polar=True)
  27.  
  28.  
  29. # Set clockwise rotation. That is:
  30. ax.set_theta_offset(pi / 2)
  31. ax.set_theta_direction(-1)
  32.  
  33.  
  34. # Set position of y-labels
  35. ax.set_rlabel_position(0)
  36.  
  37.  
  38. # Set color and linestyle of grid
  39. ax.xaxis.grid(True, color="#888888", linestyle='solid', linewidth=0.5)
  40. ax.yaxis.grid(True, color="#888888", linestyle='solid', linewidth=0.5)
  41.  
  42.  
  43. # Set number of radial axes and remove labels
  44. plt.xticks(x_as[:-1], [])
  45.  
  46. # Set yticks
  47. plt.yticks([20, 40, 60, 80, 100], ["20", "40", "60", "80", "100"])
  48.  
  49.  
  50. # Plot data
  51. ax.plot(x_as, values, linewidth=2, linestyle='solid', zorder=3)
  52.  
  53. # Fill area
  54. ax.fill(x_as, values, 'b', alpha=0.0)
  55.  
  56.  
  57. # Set axes limits
  58. plt.ylim(0, 100)
  59.  
  60.  
  61. # Draw ytick labels to make sure they fit properly
  62. for i in range(N):
  63.     angle_rad = i / float(N) * 2 * pi
  64.  
  65.     if angle_rad == 0:
  66.         ha, distance_ax = "center", 10
  67.     elif 0 < angle_rad < pi:
  68.         ha, distance_ax = "left", 1
  69.     elif angle_rad == pi:
  70.         ha, distance_ax = "center", 1
  71.     else:
  72.         ha, distance_ax = "right", 1
  73.  
  74.     ax.text(angle_rad, 100 + distance_ax, cat[i], size=10, horizontalalignment=ha, verticalalignment="center")
  75.  
  76.  
  77. # Show polar plot
  78. plt.show()
  79.  
Thanks for the help. I am a total beginner with python.
3 Weeks Ago #5

100+
P: 110
Ex.
Write at the beginning of the code.
Expand|Select|Wrap|Line Numbers
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. from math import pi
  4.  
  5. col1 = [0.0,0.0,1.0]
  6. fig = plt.figure()
  7. fig.patch.set_facecolor(col1)
3 Weeks Ago #6

P: 5
Thanks for the reply but not quite what I was after but helpful.

How do I change color of the actual chart with a sequential background, starting from blue in the middle of the circle to light blue on the outer edges?
3 Weeks Ago #7

100+
P: 110
Use matplotlib.pyplot.fill_between.
However, I do not know how to fill in a circle.
Expand|Select|Wrap|Line Numbers
  1. ax.fill_between(x_as, 0, 20, color=col1)
  2. ax.fill_between(x_as, 20, 40, color=col2)
  3. ax.fill_between(x_as, 40, 60, color=col3)
  4. ax.fill_between(x_as, 60, 80, color=col4)
  5. ax.fill_between(x_as, 80, 100, color=col5)
  6.  
2 Weeks Ago #8

P: 9
Expand|Select|Wrap|Line Numbers
  1. # Libraries
  2. import matplotlib.pyplot as plt
  3. import pandas as pd
  4. from math import pi
  5.  
  6. # Set data
  7. df = pd.DataFrame({
  8. 'group': ['A','B','C','D'],
  9. 'var1': [38, 1.5, 30, 4],
  10. 'var2': [29, 10, 9, 34],
  11. 'var3': [8, 39, 23, 24],
  12. 'var4': [7, 31, 33, 14],
  13. 'var5': [28, 15, 32, 14]
  14. })
  15.  
  16. # ------- PART 1: Define a function that do a plot for one line of the dataset!
  17.  
  18. def make_spider( row, title, color):
  19.  
  20. # number of variable
  21. categories=list(df)[1:]
  22. N = len(categories)
  23.  
  24. # What will be the angle of each axis in the plot? (we divide the plot / number of variable)
  25. angles = [n / float(N) * 2 * pi for n in range(N)]
  26. angles += angles[:1]
  27.  
  28. # Initialise the spider plot
  29. ax = plt.subplot(2,2,row+1, polar=True, )
  30.  
  31. # If you want the first axis to be on top:
  32. ax.set_theta_offset(pi / 2)
  33. ax.set_theta_direction(-1)
  34.  
  35. # Draw one axe per variable + add labels labels yet
  36. plt.xticks(angles[:-1], categories, color='grey', size=8)
  37.  
  38. # Draw ylabels
  39. ax.set_rlabel_position(0)
  40. plt.yticks([10,20,30], ["10","20","30"], color="grey", size=7)
  41. plt.ylim(0,40)
  42.  
  43. # Ind1
  44. values=df.loc[row].drop('group').values.flatten().tolist()
  45. values += values[:1]
  46. ax.plot(angles, values, color=color, linewidth=2, linestyle='solid')
  47. ax.fill(angles, values, color=color, alpha=0.4)
  48.  
  49. # Add a title
  50. plt.title(title, size=11, color=color, y=1.1)
  51.  
  52. # ------- PART 2: Apply to all individuals
  53. # initialize the figure
  54. my_dpi=96
  55. plt.figure(figsize=(1000/my_dpi, 1000/my_dpi), dpi=my_dpi)
  56.  
  57. # Create a color palette:
  58. my_palette = plt.cm.get_cmap("Set2", len(df.index))
  59.  
  60. # Loop to plot
  61. for row in range(0, len(df.index)):
  62. make_spider( row=row, title='group '+df['group'][row], color=my_palette(row))
I hope this code helps!
2 Weeks Ago #9

Post your reply

Sign in to post your reply or Sign up for a free account.