各坐标不同最大值/最小值
法一: (推荐)
#!/usr/local/bin/python3
# @IDE: PyCharm
# @.py: radar_p
#-*- coding : utf-8
# @Author:may
# @Time: 2024/5/20
# pip3 install pyecharts
import pyecharts.options as op
from pyecharts.charts import Radar
# 传入多维数据,数据点最多6个
v1 = [[8.5, 75, 90, 70, 90]]
v2 = [[8.7, 72, 95, 75, 95]]
v3 = [[8.0, 95, 70, 85, 80]]
v4 = [[8.5, 90, 75, 85, 75]]
v5 = [[8.5, 85, 80, 80, 85]]
v6 = [[7.5, 90, 82, 85, 88]]
# 调整雷达各维度的范围大小,维度要求四维以上
x_schema = [
{"name": "舒适性", "max": 9, "min": 8, "color": 'black', "font_size": 18},
{"name": "油耗", "max": 100, "min": 65, "color": 'black', "font_size": 18},
{"name": "性能", "max": 100, "min": 65, "color": 'black', "font_size": 18},
{"name": "安全", "max": 100, "min": 65, "color": 'black', "font_size": 18},
{"name": "操控", "max": 100, "min": 65, "color": 'black', "font_size": 18}
]
# 画图
radar_x = Radar()
radar_x.add_schema(x_schema)
radar_x.add('帕萨特', v1, color='red').set_colors(['red'])
radar_x.add('迈腾', v2, color='green').set_colors(['green'])
radar_x.add('凯美瑞', v3, color='orange').set_colors(['orange'])
radar_x.add('亚洲龙', v4, color='blue').set_colors(['blue'])
radar_x.add('天籁', v5, color='purple').set_colors(['purple'])
radar_x.add('雅阁', v6, color='yellow').set_colors(['yellow'])
radar_x.set_global_opts(
title_opts=op.TitleOpts(title="car_type", pos_right="center"),
legend_opts=op.LegendOpts(legend_icon="roundRect", align="left", pos_left='7%',
pos_bottom='14%', orient='vertical')
)
radar_x.render("car_type.html")
法二:
#!/usr/local/bin/python3
# @IDE: PyCharm
# @.py: test
# -*- coding : utf-8
# @Author:may
# @Time: 2024/5/18
import numpy as np
import matplotlib
# matplotlib.use('TkAgg') OS报错macOS 12 (1207) or later required, have instead 12 (1206) !
matplotlib.use('Qt5Agg')
import matplotlib.pyplot as plt
class Radar(object):
def __init__(self, figure, title, labels, epoch, rect=None):
if rect is None:
rect = [0.05, 0.05, 0.9, 0.9]
self.n = len(title)
self.angles = np.arange(0, 360, 360.0 / self.n)
self.axes = [figure.add_axes(rect, projection='polar', label='axes%d' % i) for i in range(self.n)]
self.ax = self.axes[0]
self.ax.set_thetagrids(self.angles, labels=title, fontsize=14)
for ax in self.axes[1:]:
ax.patch.set_visible(False)
ax.grid(False)
ax.xaxis.set_visible(False)
for ax, angle, label, i in zip(self.axes, self.angles, labels, epoch):
ax.set_rgrids(i[1:], angle=angle, labels=label) # 这里的range(1,6)应该是对应标签的个数
ax.spines['polar'].set_visible(False)
ax.set_ylim(i[0], i[7]) # 这里应该是对应轴的刻度
def plot(self, values, *args, **kw):
angle = np.deg2rad(np.r_[self.angles, self.angles[0]])
limits = []
# 把value的值进行一个换算
limits.append(values[1] - 5)
limits.append(values[2] - 50)
limits.append((values[3] - 45) / 5)
limits.append((values[4] + 15) / 10)
values[1] = (limits[0]) * 0.5 + 4.5
values[2] = (limits[1]) * 0.5 + 4.5
values[3] = (limits[2]) * 0.5 + 4.5
values[4] = (limits[3]) * 0.5 + 4.5
values = np.r_[values, values[0]]
self.ax.plot(angle, values, *args, **kw)
if __name__ == '__main__':
fig = plt.figure(figsize=(8, 8))
# tit = list(('人均GDP/万元','年均GDP增速','第三产业占比','城镇化率','碳排放强度下降幅度'))
tit = ["峰值速率(Gbit/s)", "移动性(km/h)", "流量密度(10Tbit/(s.km))", "端到端时延(ms)", "连接数密度(设备/km)"]
lab = [
list(('4.5', '5.0', '5.5', '6.0', '6.5', '7.0', '7.5')),
list(('5', '6', '7', '8', '9', '10', '11')),
list(('50', '51', '52', '53', '54', '55', '56')),
list(('45', '50', '55', '60', '65', '70', '75')),
list(('-15', '-5', '5', '15', '25', '35', '45')),
]
epo = [
[4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5],
[4, 5, 6, 7, 8, 9, 10, 11],
[49, 50, 51, 52, 53, 54, 55, 56],
[40, 45, 50, 55, 60, 65, 70, 75],
[-25, -15, -5, 5, 15, 25, 35, 45]
]
# plt.rcParams['font.sans-serif'] = ['SimHei.ttf']
plt.rcParams['font.sans-serif'] = ['Songti SC'] # 用来正常显示中文标签(macO系统))
radar = Radar(fig, tit, lab, epo)
# radar.plot([4.5,5,50,45,-15],'-', lw=2, color='r', alpha=0.4, label='全国平均') #范例
# 绘制图3
radar.plot([4.58, 7.52, 51.20, 49.67, 19.72], '-', lw=2, color='r', alpha=0.4, label='A')
radar.plot([7.03, 7.60, 54.27, 60.60, 13.34], '-', lw=2, color='b', alpha=0.4, label='B')
radar.ax.legend()
plt.savefig('fig-3.png', dpi=300)
plt.show()
各坐标相同大小
import pygal
radar_chart = pygal.Radar()
radar_chart.title = 'V8 benchmark results'
radar_chart.x_labels = ['Richards', 'DeltaBlue', 'Crypto', 'RayTrace', 'EarleyBoyer', 'RegExp', 'Splay', 'NavierStokes']
radar_chart.add('Chrome', [6395, 8212, 7520, 7218, 12464, 1660, 2123, 8607])
radar_chart.add('Firefox', [7473, 8099, 11700, 2651, 6361, 1044, 3797, 9450])
radar_chart.add('Opera', [3472, 2933, 4203, 5229, 5810, 1828, 9013, 4669])
radar_chart.add('IE', [43, 41, 59, 79, 144, 136, 34, 102])
radar_chart.render_to_file('bar_chart.svg')