473,320 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Adding color to radar chart in python

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.
Mar 5 '20 #1
8 4273
SioSio
272 256MB
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.  
Mar 6 '20 #2
swisha
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
Mar 6 '20 #3
SioSio
272 256MB
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")
Mar 6 '20 #4
swisha
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.
Mar 6 '20 #5
SioSio
272 256MB
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)
Mar 6 '20 #6
swisha
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?
Mar 7 '20 #7
SioSio
272 256MB
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.  
Mar 9 '20 #8
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!
Mar 10 '20 #9

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

Similar topics

1
by: ghostrider | last post by:
I was wondering if someone could give me some pointers on how to add a pie chart to an existing program. Everything that I've tried will not work for me. So if there is some out there that can point...
1
by: FranMansfield | last post by:
Hi there, I'm trying to make it so that the questions on the email that gets emailed back to me from my PHP form, or the answers, are in a different color. I've tried adding , or <font...
135
by: robinsiebler | last post by:
I've never had any call to use floating point numbers and now that I want to, I can't! *** Python 2.5.1 (r251:54863, May 1 2007, 17:47:05) on win32. *** 0.29999999999999999 0.29999999999999999
1
by: r0ssc0vich | last post by:
Hi! I'm a newbie I’m afraid :) I work for a charity which cares for ex-offenders inside and outside of prison. I've built a basic access 2003 data base to keep track of our clients, their status,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.