带颜色条的圆形图 [英] Circle Plot with Color Bar

查看:165
本文介绍了带颜色条的圆形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用彩条绘制圆形图,就像这样:

I'm trying to do a Circle Plot with a Color Bar, almost like this:

但是,彩条的最小值当前为1;最小值为1.我希望能够将其设置为0.

However, the minimum value of the colour bar is currently 1; I would like to be able to set it to 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()

对这个原始问题的信任: 绘制没有圆的圆填充,颜色和大小取决于使用散点图的变量

Credit to this original question: Plotting circles with no fill, colour & size depending on variables using scatter

推荐答案

感谢alec_djinn这个答案可以做到:

Thanks to alec_djinn this answer does:

  • 设置分钟和分钟彩条的最大值
  • 将圆圈(变量C)的颜色控制在与颜色条相同的范围内

import pandas            as pd
import matplotlib.pyplot as plt
import matplotlib.cm     as cm
from sklearn import preprocessing
from matplotlib.colors import Normalize

df = pd.DataFrame({'A':[1,2,1,2,3,4,2,1,4], 
                   'B':[3,2,5,1,2,4,5,2,3], 
                   'C':[4,2,4,1,3,3,4,2,1]})

# set the Colour
x              = df[['C']].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[0]
cmap           = cm.jet # Use the same Cmap

# Set the Colour Scale
vmin = 0
vmax = 5
norm = Normalize(vmin, vmax)

# Graph
plt.figure()
ax = plt.gca()
for a, b, c in zip(df['A'], df['B'], df['C']):
    circle = plt.Circle((a, 
                         b), 
                         1, # Size
                         color=cmap(norm(c)), 
                         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', # Use the same Cmap
                 vmin = vmin,
                 vmax = vmax,
                 facecolors='none')
plt.grid()
cbar = plt.colorbar(sc)
cbar.set_label('C', rotation=270, labelpad=20)

plt.show()

这篇关于带颜色条的圆形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆