python - 带颜色条的圆图

标签 python dataframe geometry colorbar scatter

我正在尝试用颜色条绘制圆图,几乎像这样:

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()

归功于这个原始问题: Plotting circles with no fill, colour & size depending on variables using scatter

最佳答案

只需在 plt.scatter() 中添加 vminvmax 参数。

sc = plt.scatter(df['A'], 
                 df['B'], 
                 s=0, 
                 c=c1, 
                 cmap='jet',
                 vmin = 0,
                 vmax = 4,
                 facecolors='none')

enter image description here

如果您想根据颜色图调整圆圈的颜色,则需要使用 `Normalize(vmin, vmax) 并将颜色图传递给具有归一化值的圆图。

代码如下:

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,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]
cmap = cm.jet
vmin = 0
vmax = 5 #your max Y is 5, not 4
norm = Normalize(vmin, vmax)

# Graph
plt.figure()
ax = plt.gca()
for a, b in zip(df['A'], df['B']):
    circle = plt.Circle((a, 
                         b), 
                         1, # Size
                         color=cmap(norm(b)), 
                         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',
                 vmin = vmin,
                 vmax = vmax,
                 facecolors='none')
plt.grid()

cbar = plt.colorbar(sc)
cbar.set_label('C', rotation=270, labelpad=10)

plt.show()

enter image description here

关于python - 带颜色条的圆图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51020192/

相关文章:

python - 如何消除该函数中的一个参数?

python - 使用另一个 DataFrame 创建或修改 DataFrame

c# - 计算沿直线 A-B 与 A 的给定距离处的点

python - 如何在Python数据框中将一些值剪切到不同的列中?

python - 我需要根据列上的值从 Pandas 数据框中制作真值表

algorithm - 在球坐标系中最接近点的网格方 block

python - Python中的最小曲面解决方案

javascript - (Web)套接字连接发送 header 而不是字符串

python - 谷歌联系人悬停无法与 python selenium webdriver 一起使用

python - 使 Emacs 在 Python 交互模式下使用 UTF-8