如何将轮盘赌轮背景图像添加到matplotlib雷达图 [英] How to add Roulette Wheel background image to matplotlib Radar Chart
问题描述
我有一个程序,可以将轮盘赌轮的数字命中频率绘制到雷达图上.我想要一张轮圈/边框的雷达边缘/雷达图的外部的图像.我已经尝试过在线采购代码,但出现错误:
I have a program that plots the hit frequency of the numbers of a Roulette Wheel onto a Radar chart. I would like to have an image of the outer rim of the wheel circle/border the outer part of the Radar chart. I have tried sourcing code online but I'm getting the error:
raise TypeError("Image data cannot be converted to float")
TypeError: Image data cannot be converted to float
我正在使用的代码示例(请参见下文)产生以下图:
A sample of the code I'm using (see below) produces the following plot:
from math import pi
import matplotlib.pyplot as plt
from scipy.misc import imread
import matplotlib.cbook as cbook
# Numbers of European Roulette Wheel
cat = [0, 32, 15, 19, 4, 21, 2, 25, 17, 34, 6, 27, 13, 36, 11, 30, 8, 23, 10, 5, 24, 16, 33, 1, 20, 14, 31, 9, 22, 18, 29, 7, 28, 12, 35, 3, 26]
# Hit frequency of each number
values = [108, 116, 102, 100, 102, 107, 115, 114, 110, 102, 114, 101, 121, 103, 121, 105, 104, 117, 108, 121, 122, 97, 114, 122, 98, 105, 114, 111, 129, 123, 107, 112, 124, 102, 124, 109, 107]
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=':', linewidth=0.5)
ax.yaxis.grid(True, color="#888888", linestyle=':', linewidth=0.5)
# Set number of radial axes and remove labels
plt.xticks(x_as[:-1], [])
# Set yticks
tickInt = []
tickStr = []
y = 0
maxHitFrequency = max(values)
while y <= maxHitFrequency:
tickInt.append(y)
tickStr.append(str(y))
y += 25
plt.yticks(tickInt, tickStr)
# Plot data
ax.plot(x_as, values, linewidth=0, linestyle='solid', zorder=3)
# Fill area
ax.fill(x_as, values, 'b', alpha=0.45, color="#006600")
# Set axes limits
plt.ylim(0, y)
plt.show()
我想让地块上贴上类似下面的图像:
I'd like to have the plot bordered with an image similar to this:
以下是我尝试无法使用的代码:
Below is the code which I've tried to no avail:
# Display Image
datafile = cbook.get_sample_data('D:\AR Wheel Vector.jpg')
img = imread(datafile)
plt.imshow(img)
推荐答案
我认为很难将图像精确定位在需要适应极坐标图的位置.因此,我宁愿尝试在matplotlib图中重新创建轮盘.
I think it will be difficult to position the image exactly where it needs to be to fit the polar plot. I would hence rather try to recreate the roulette board in the matplotlib plot.
主要想法是创建许多具有不同颜色的矩形,并将数字作为文本放入其中.通过使用theta轴的坐标轴变换ax.get_xaxis_tranform()
,可以轻松完成许多操作,例如,角坐标是角度(数据单位),径向坐标是轴坐标(轴内部的0
至1
).
The idea is mainly to create a lot of rectangles with alternating color and place the numbers as text into them. Much of this is easily done by using the axis tranform of the theta axis, ax.get_xaxis_tranform()
, such that angular coordinates are the angles (data units) and radial coordinates are axes coordinates (0
to 1
inside the axes).
import numpy as np
import matplotlib.pyplot as plt
# Numbers of European Roulette Wheel
cat = [0, 32, 15, 19, 4, 21, 2, 25, 17, 34, 6, 27, 13, 36, 11, 30, 8, 23, 10, 5,
24, 16, 33, 1, 20, 14, 31, 9, 22, 18, 29, 7, 28, 12, 35, 3, 26]
# Hit frequency of each number
values = [108, 116, 102, 100, 102, 107, 115, 114, 110, 102, 114, 101, 121, 103,
121, 105, 104, 117, 108, 121, 122, 97, 114, 122, 98, 105, 114, 111, 129,
123, 107, 112, 124, 102, 124, 109, 107]
N = len(cat)
x_as = [n / float(N) * 2 * np.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(np.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=':', linewidth=0.5)
ax.yaxis.grid(True, color="#888888", linestyle=':', linewidth=0.5)
# Set number of radial axes and remove labels
plt.xticks(x_as[:-1], [])
## Set yticks
yticks = np.arange(0,max(values),25)
plt.yticks(yticks, yticks)
# Plot data
ax.plot(x_as, values, linewidth=0, linestyle='solid', zorder=3)
# Fill area
ax.fill(x_as, values, 'b', alpha=0.45, color="#006600")
# Set axes limits
plt.ylim(0, np.ceil(max(values)/25.)*25)
x_bin = np.array(x_as)[:-1]-np.diff(x_as)[0]/2.
c = ["#fe0000" if i%2 else "#000000" for i in range(len(x_bin))]
c[0] = "#007f0e"
for i in range(len(x_bin)):
rec = plt.Rectangle([x_bin[i],1],np.diff(x_as)[0],0.22 ,
transform=ax.get_xaxis_transform(),
color=c[i], clip_on=False)
ax.add_patch(rec)
ax.text(x_as[i], 1.1, cat[i],transform=ax.get_xaxis_transform(),
ha="center", va="center", color="w", size=12, weight='bold', family="serif",
rotation_mode="anchor", rotation=-np.rad2deg(x_as[i]))
ang = np.linspace(0,2*np.pi,200)
kw = dict(clip_on=False, lw=3,transform=ax.get_xaxis_transform())
ax.plot(ang,np.ones_like(ang), color="k", **kw)
ax.plot(ang,np.ones_like(ang)*1.22, color="w",**kw)
plt.show()
这篇关于如何将轮盘赌轮背景图像添加到matplotlib雷达图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!