根据变量使用s绘制无填充、颜色和大小的圆

2024-05-19 01:46:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我必须在绘图上显示的信息是2个坐标:大小和颜色(无填充)。颜色很重要,因为我需要一个彩色地图类型的图形来显示信息取决于颜色值。

我尝试了两种不同的方法:

  1. 创建特定的圆并添加各个圆。

    circle1 = plt.Circle(x, y, size, color='black', fill=False)
            ax.add_artist(circle1)
    

这种方法的问题是我找不到一种方法来根据颜色值设置颜色。i、 e.对于0-1的值范围,我希望0为完全蓝色,而1为完全红色,因此介于两者之间的是不同的紫色,其红色/蓝色取决于颜色值的高低。

  1. 之后,我尝试使用散布函数:

    size.append(float(Info[i][8]))
    plt.scatter(x, y, c=color, cmap='jet', s=size, facecolors='none')
    

这个方法的问题是大小似乎没有变化,这可能是我创建数组大小的方式造成的。因此,如果我用一个大数字替换大小,则图将以圆圈的形式显示。facecolours = 'none'仅用于绘制周长。


Tags: 方法none信息图形绘图类型size颜色
2条回答

@Raket Makhim写道:

"I'm only getting one colour"

&;@pault回答:

"Try scaling your colors to the range 0 to 1." 

我已经实现了:

enter image description here

(但是,颜色栏的最小值当前为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()

我相信两种方法都能达到你想要的效果。先画未填充的圆,然后用相同的点绘制散点图。对于散点图,使大小为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()

我的一次跑步中的绘图示例:

Example plot output

相关问题 更多 >

    热门问题