Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Customize spider chart
#1
I want to change the radar chart from a circle to a polygon side shape.
How do I do this using this example from matplotlib website:
# Plots a radar chart.
 
from math import pi
import matplotlib.pyplot as plt
 
 
# Set data
cat = ['Speed', 'Reliability', 'Comfort', 'Safety', 'Effieciency']
values = [90, 60, 65, 70, 40]
 
N = len(cat)
 
x_as = [n / float(N) * 2 * pi for n in range(N)]
 
# Because our chart will be circular we need to append a copy of the first 
# value of each list at the end of each list with data
values += values[:1]
x_as += x_as[:1]
 
 
# Set color of axes
plt.rc('axes', linewidth=0.5, edgecolor="#888888")
 
 
# Create polar plot
ax = plt.subplot(111, polar=True)
 
 
# Set clockwise rotation. That is:
ax.set_theta_offset(pi / 2)
ax.set_theta_direction(-1)
 
 
# Set position of y-labels
ax.set_rlabel_position(0)
 
 
# Set color and linestyle of grid
ax.xaxis.grid(True, color="#888888", linestyle='solid', linewidth=0.5)
ax.yaxis.grid(True, color="#888888", linestyle='solid', linewidth=0.5)
 
 
# Set number of radial axes and remove labels
plt.xticks(x_as[:-1], [])
 
# Set yticks
plt.yticks([20, 40, 60, 80, 100], ["20", "40", "60", "80", "100"])
 
 
# Plot data
ax.plot(x_as, values, linewidth=2, linestyle='solid', zorder=3)
 
# Fill area
ax.fill(x_as, values, 'b', alpha=0.0)
 
 
# Set axes limits
plt.ylim(0, 100)
 
 
# Draw ytick labels to make sure they fit properly
for i in range(N):
    angle_rad = i / float(N) * 2 * pi
 
    if angle_rad == 0:
        ha, distance_ax = "center", 10
    elif 0 < angle_rad < pi:
        ha, distance_ax = "left", 1
    elif angle_rad == pi:
        ha, distance_ax = "center", 1
    else:
        ha, distance_ax = "right", 1
 
    ax.text(angle_rad, 100 + distance_ax, cat[i], size=10, horizontalalignment=ha, verticalalignment="center")
 
 
# Show polar plot
plt.show()
Reply
#2
look at https://matplotlib.org/3.1.0/gallery/spe...chart.html
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
I am trying to figure out how to change elements of code for this spider chart. 2 things I am trying to change.

1. I want to make this chart sided (not circular).
2. I want to make the green color darker in the middle, while fading out white towards the edges. I have only managed to do that from top to bottom.

Here is the code I have managed so far.
from math import pi
import matplotlib.pyplot as plt
from matplotlib.patches import Circle, RegularPolygon
import pandas as pd
import numpy as np
from math import pi

def gradient_image(ax, extent, direction=0.3, cmap_range=(0, 1), **kwargs):
    
    phi = direction * np.pi / 2
    v = np.array([np.cos(phi), np.sin(phi)])
    X = np.array([[v @ [1, 0], v @ [1, 1]],
                  [v @ [0, 0], v @ [0, 1]]])
    a, b = cmap_range
    X = a + (b - a) / X.max() * X
    im = ax.imshow(X, extent=extent, interpolation='bicubic',
                   vmin=0, vmax=1, **kwargs)
    return im

# Set data
cat = ['A', 'B', 'C', 'D', 'E', 'F','G','H','I']
values = [16, 14, 14, 10, 9.5, 5, 2, 1, 0]

N = len(cat)

x_as = [n / float(N) * 2 * pi for n in range(N)]

# Because our chart will be circular we need to append a copy of the first 
# value of each list at the end of each list with data
values += values[:1]
x_as += x_as[:1]

# Set color of axes
plt.rc('axes', linewidth=0.5, edgecolor="#888888")

# Create polar plot
ax = plt.figure(figsize=(10, 6))
ax = plt.subplot(polar=True)

# Set clockwise rotation. That is:
ax.set_theta_offset(pi / 2)
ax.set_theta_direction(-1)

# Set position of y-labels
ax.set_rlabel_position(0)

# Setting the background colour 
ax.set_facecolor("grey")

# Set color and linestyle of grid
ax.xaxis.grid(True, color="#888888", linestyle='solid', linewidth=0.5)
ax.yaxis.grid(True, color="#888888", linestyle='solid', linewidth=0.5)

# Set number of radial axes and remove labels
plt.xticks(x_as[:-1], [])

# Set yticks
plt.yticks([0, 5, 10, 15, 20, 25, 30], ["0%", "5%", "10%", "15%", "20%", "25%", "30%"], color="black", size=10)


# Plot data
ax.plot(x_as, values, linewidth=2, linestyle='solid')

# Fill area
ax.fill(x_as, values, 'red', alpha=0.1)


# Set axes limits
plt.ylim(0, 30)

# Draw ytick labels to make sure they fit properly
for i in range(N):
    angle_rad = i / float(N) * 2 * pi

    if angle_rad == 0:
        ha, distance_ax = "center", 1
    elif 0 < angle_rad < pi:
        ha, distance_ax = "left", 1
    elif angle_rad == pi:
        ha, distance_ax = "center", 1
    else:
        ha, distance_ax = "right", 1

    ax.text(angle_rad, 35 + distance_ax, cat[i], size=10, horizontalalignment=ha, verticalalignment="center")

gradient_image(ax, direction=0, extent=(0, 1, 0, 1), transform=ax.transAxes,
               cmap=plt.cm.Greens, cmap_range=(0.1, 0.6))

# Show polar plot
plt.show()
Any help is appreciated.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  adding the customize dataset to BERT alicenguyen 2 1,056 Jul-06-2022, 08:06 AM
Last Post: Larz60+
  How to customize each x-axis separately? Mark17 4 1,966 Mar-10-2022, 02:56 PM
Last Post: Mark17
  Customize Python Keywords for IDLE alanvers 0 1,992 Apr-03-2021, 10:56 AM
Last Post: alanvers
  pytest-html report customize manoj 4 14,268 Nov-26-2019, 09:10 AM
Last Post: manojshetty

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020