本文介绍了绘制没有填充,颜色和颜色的圆圈大小取决于使用散点的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The information I have to show on a plot are 2 coordinates: size & colour (no fill). The colour is important as I need a colormap type of graph to display the information depending on a colour value.

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

I went about trying two different ways of doing this:

  1. 创建特定的圈子并添加单个圈子.

  1. Create specific circles and add the individual circles.

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

此方法的问题是我找不到根据颜色值设置颜色的方法.也就是说,对于0-1的值范围,我希望0为全蓝色,而1为全红色,因此介于两者之间的是深浅不一的紫色,其红色/蓝色取决于颜色值的高/低.

The problem with this method was that I could not find a way to set the colour depending on a colour value. i.e. for a value range of 0-1, I want 0 to be fully blue while 1 to be fully red hence in between are different shades of purple whose redness/blueness depend on how high/low the colour value is.

  1. 之后,我尝试使用分散功能:

  1. After that I tried using the scatter function:

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

此方法的问题在于大小似乎没有变化,这可能是我创建数组大小的原因.因此,如果我将尺寸替换为一个大数字,该图将显示为彩色圆圈. facecolours ='none'仅用于绘制圆周.

The problem with this method was that the size did not seem to vary, it could possibly be cause of the way I've created the array size. Hence if I replace the size with a big number the plot shows coloured in circles. The facecolours = 'none' was meant to plot the circumference only.

推荐答案

我相信同时使用这两种方法都可以实现您想要的目标.首先绘制未填充的圆,然后使用相同的点绘制散点图.对于散点图,将大小设为0,但使用它来设置颜色条.

I believe doing both approaches may achieve what you are trying to do. First draw the unfilled circles, then do a scatter plot with the same points. For the scatter plots, make the size 0 but use it to set the colorbar.

请考虑以下示例:

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 from one of my runs:

这篇关于绘制没有填充,颜色和颜色的圆圈大小取决于使用散点的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 04:28