使用散点图根据变量绘制没有填充、颜色和大小的圆
Plotting circles with no fill, colour & size depending on variables using scatter
我必须在绘图上显示的信息是 2 个坐标:大小和颜色(无填充)。颜色很重要,因为我需要一个颜色图类型的图表来根据颜色值显示信息。
我尝试了两种不同的方法:
创建特定的圈子并添加各个圈子。
circle1 = plt.Circle(x, y, size, color='black', fill=False)
ax.add_artist(circle1)
这个方法的问题是我找不到根据颜色值设置颜色的方法。即对于 0-1 的值范围,我希望 0 为全蓝色,而 1 为全红色,因此介于两者之间的是不同深浅的紫色,其 redness/blueness 取决于 high/low 颜色值的大小。
之后我尝试使用分散函数:
size.append(float(Info[i][8]))
plt.scatter(x, y, c=color, cmap='jet', s=size, facecolors='none')
此方法的问题是大小似乎没有变化,这可能是我创建数组大小的方式的原因。因此,如果我用一个大数字替换大小,则绘图显示为彩色圆圈。 facecolours = 'none'
仅用于绘制圆周。
我相信这两种方法都可以实现您想要做的事情。首先绘制未填充的圆圈,然后用相同的点绘制散点图。对于散点图,将大小设置为 0,但使用它来设置颜色条。
考虑以下示例:
import numpy as np
from matplotlib import pyplot as plt
import matplotlib.cm as cm
%matplotlib inline
# generate some random data
npoints = 5
x = np.random.randn(npoints)
y = np.random.randn(npoints)
# make the size proportional to the distance from the origin
s = [0.1*np.linalg.norm([a, b]) for a, b in zip(x, y)]
s = [a / max(s) for a in s] # scale
# set color based on size
c = s
colors = [cm.jet(color) for color in c] # gets the RGBA values from a float
# create a new figure
plt.figure()
ax = plt.gca()
for a, b, color, size in zip(x, y, colors, s):
# plot circles using the RGBA colors
circle = plt.Circle((a, b), size, color=color, fill=False)
ax.add_artist(circle)
# you may need to adjust the lims based on your data
minxy = 1.5*min(min(x), min(y))
maxxy = 1.5*max(max(x), max(y))
plt.xlim([minxy, maxxy])
plt.ylim([minxy, maxxy])
ax.set_aspect(1.0) # make aspect ratio square
# plot the scatter plot
plt.scatter(x,y,s=0, c=c, cmap='jet', facecolors='none')
plt.grid()
plt.colorbar() # this works because of the scatter
plt.show()
我的一次运行中的示例图:
@Raket Makhim 写道:
"I'm only getting one colour"
&@pault 回复:
"Try scaling your colors to the range 0 to 1."
我已经实现了:
(不过目前彩条的最小值是1,我希望能设置成0,我再问一个新问题)
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from sklearn import preprocessing
df = pd.DataFrame({'A':[1,2,1,2,3,4,2,1,4],
'B':[3,1,5,1,2,4,5,2,3],
'C':[4,2,4,1,3,3,4,2,1]})
# set the Colour
x = df.values
min_max_scaler = preprocessing.MinMaxScaler()
x_scaled = min_max_scaler.fit_transform(x)
df_S = pd.DataFrame(x_scaled)
c1 = df['C']
c2 = df_S[2]
colors = [cm.jet(color) for color in c2]
# Graph
plt.figure()
ax = plt.gca()
for a, b, color in zip(df['A'], df['B'], colors):
circle = plt.Circle((a,
b),
1, # Size
color=color,
lw=5,
fill=False)
ax.add_artist(circle)
plt.xlim([0,5])
plt.ylim([0,5])
plt.xlabel('A')
plt.ylabel('B')
ax.set_aspect(1.0)
sc = plt.scatter(df['A'],
df['B'],
s=0,
c=c1,
cmap='jet',
facecolors='none')
plt.grid()
cbar = plt.colorbar(sc)
cbar.set_label('C', rotation=270, labelpad=10)
plt.show()
我必须在绘图上显示的信息是 2 个坐标:大小和颜色(无填充)。颜色很重要,因为我需要一个颜色图类型的图表来根据颜色值显示信息。
我尝试了两种不同的方法:
创建特定的圈子并添加各个圈子。
circle1 = plt.Circle(x, y, size, color='black', fill=False) ax.add_artist(circle1)
这个方法的问题是我找不到根据颜色值设置颜色的方法。即对于 0-1 的值范围,我希望 0 为全蓝色,而 1 为全红色,因此介于两者之间的是不同深浅的紫色,其 redness/blueness 取决于 high/low 颜色值的大小。
之后我尝试使用分散函数:
size.append(float(Info[i][8])) plt.scatter(x, y, c=color, cmap='jet', s=size, facecolors='none')
此方法的问题是大小似乎没有变化,这可能是我创建数组大小的方式的原因。因此,如果我用一个大数字替换大小,则绘图显示为彩色圆圈。 facecolours = 'none'
仅用于绘制圆周。
我相信这两种方法都可以实现您想要做的事情。首先绘制未填充的圆圈,然后用相同的点绘制散点图。对于散点图,将大小设置为 0,但使用它来设置颜色条。
考虑以下示例:
import numpy as np
from matplotlib import pyplot as plt
import matplotlib.cm as cm
%matplotlib inline
# generate some random data
npoints = 5
x = np.random.randn(npoints)
y = np.random.randn(npoints)
# make the size proportional to the distance from the origin
s = [0.1*np.linalg.norm([a, b]) for a, b in zip(x, y)]
s = [a / max(s) for a in s] # scale
# set color based on size
c = s
colors = [cm.jet(color) for color in c] # gets the RGBA values from a float
# create a new figure
plt.figure()
ax = plt.gca()
for a, b, color, size in zip(x, y, colors, s):
# plot circles using the RGBA colors
circle = plt.Circle((a, b), size, color=color, fill=False)
ax.add_artist(circle)
# you may need to adjust the lims based on your data
minxy = 1.5*min(min(x), min(y))
maxxy = 1.5*max(max(x), max(y))
plt.xlim([minxy, maxxy])
plt.ylim([minxy, maxxy])
ax.set_aspect(1.0) # make aspect ratio square
# plot the scatter plot
plt.scatter(x,y,s=0, c=c, cmap='jet', facecolors='none')
plt.grid()
plt.colorbar() # this works because of the scatter
plt.show()
我的一次运行中的示例图:
@Raket Makhim 写道:
"I'm only getting one colour"
&@pault 回复:
"Try scaling your colors to the range 0 to 1."
我已经实现了:
(不过目前彩条的最小值是1,我希望能设置成0,我再问一个新问题)
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from sklearn import preprocessing
df = pd.DataFrame({'A':[1,2,1,2,3,4,2,1,4],
'B':[3,1,5,1,2,4,5,2,3],
'C':[4,2,4,1,3,3,4,2,1]})
# set the Colour
x = df.values
min_max_scaler = preprocessing.MinMaxScaler()
x_scaled = min_max_scaler.fit_transform(x)
df_S = pd.DataFrame(x_scaled)
c1 = df['C']
c2 = df_S[2]
colors = [cm.jet(color) for color in c2]
# Graph
plt.figure()
ax = plt.gca()
for a, b, color in zip(df['A'], df['B'], colors):
circle = plt.Circle((a,
b),
1, # Size
color=color,
lw=5,
fill=False)
ax.add_artist(circle)
plt.xlim([0,5])
plt.ylim([0,5])
plt.xlabel('A')
plt.ylabel('B')
ax.set_aspect(1.0)
sc = plt.scatter(df['A'],
df['B'],
s=0,
c=c1,
cmap='jet',
facecolors='none')
plt.grid()
cbar = plt.colorbar(sc)
cbar.set_label('C', rotation=270, labelpad=10)
plt.show()