【问题标题】:divide different colours in color bar在颜色栏中划分不同的颜色
【发布时间】:2022-01-11 01:18:07
【问题描述】:

我正在尝试在 matplotlib 上绘制一些值。这是我迄今为止所取得的成就。

问题是颜色条只显示一些颜色。如何为每个游戏条目推送不同的颜色?

if __name__ == '__main__':
    # reading excel file
    games_male_attendance = collections.OrderedDict()
    games_female_attendance = collections.OrderedDict()
    df = pd.read_excel("Olympic-dataset.xlsx", usecols=["Game_Discipline", "Male", "Female"])

for index, row in df.iterrows():
    game_name = row["Game_Discipline"]
    male_attendance = row["Male"]
    female_attendance = row["Female"]

    if game_name not in games_male_attendance:
        games_male_attendance[game_name] = male_attendance

    if game_name not in games_female_attendance:
        games_female_attendance[game_name] = female_attendance

list_male_attendance = list(games_male_attendance.values())
list_female_attendance = list(games_female_attendance.values())
classes = list(games_male_attendance.keys())

# set colors and legends
colors = plt.cm.get_cmap('tab20c')

indexes = []
for i in range(len(classes)):
    indexes.append(i)

scatter = plt.scatter(y=list_male_attendance, x=list_female_attendance, c=range(len(classes)), cmap=colors, vmin=0, vmax=len(classes))
cbar = plt.colorbar(scatter)
cbar.ax.get_yaxis().set_ticks(indexes, labels=classes)
plt.ylabel("male attendance")
plt.xlabel("female attendance")

plt.show()

数据:

print(list_male_attendance)
[131, 98, 265, 144, 168, 42, 193, 0, 144, 1072, 0, 178, 86, 201, 344, 65, 187, 99, 64, 108, 71, 87, 48, 175, 192, 16, 25, 418, 55, 41, 146, 20, 126, 24, 151, 144, 125, 97, 99, 60, 40, 36, 38, 32, 9, 20]

print(list_female_attendance)
[70, 98, 257, 144, 168, 40, 96, 96, 90, 969, 105, 178, 86, 192, 264, 65, 102, 98, 64, 107, 72, 86, 48, 175, 192, 16, 25, 361, 55, 41, 122, 20, 123, 24, 146, 144, 73, 94, 90, 60, 40, 36, 38, 32, 10, 20]

print(classes)
['Cycling Road', 'Artistic Gymnastics', 'Rowing', 'Basketball', 'Handball', 'Karate', 'Wrestling', 'Rhythmic Gymnastics', 'Baseball/Softball', 'Athletics', 'Artistic Swimming', 'Shooting', 'Table Tennis', 'Judo', 'Football', 'Taekwondo', 'Boxing', 'Weightlifting', 'Archery', 'Fencing', 'Diving', 'Badminton', 'Beach Volleyball', 'Sailing', 'Hockey', 'Trampoline Gymnastics', 'Marathon Swimming', 'Swimming', 'Triathlon', 'Canoe Slalom', 'Water Polo', 'Surfing', 'Canoe Sprint', 'Cycling BMX Racing', 'Rugby Sevens', 'Volleyball', 'Equestrian', 'Tennis', 'Cycling Track', 'Golf', 'Skateboarding', 'Modern Pentathlon', 'Cycling Mountain Bike', '3x3 Basketball', 'Cycling BMX Freestyle', 'Sport Climbing']

【问题讨论】:

  • 你不能拥有那么多视觉上可区分的颜色。也许改用标记?也许标记与颜色相结合?

标签: python matplotlib scatter-plot


【解决方案1】:

tab20c 颜色条只有 20 种颜色,比您的类别数量少。您可以做的一件事是将多个颜色图连接在一起并将其用于您的绘图。我使用了this 的方法并将其应用于您的情况。您可以在下面找到代码:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import ListedColormap, BoundaryNorm

fig=plt.figure(figsize=(15,12))

list_male_attendance=[131, 98, 265, 144, 168, 42, 193, 0, 144, 1072, 0, 178, 86, 201, 344, 65, 187, 99, 64, 108, 71, 87, 48, 175, 192, 16, 25, 418, 55, 41, 146, 20, 126, 24, 151, 144, 125, 97, 99, 60, 40, 36, 38, 32, 9, 20]

list_female_attendance=[70, 98, 257, 144, 168, 40, 96, 96, 90, 969, 105, 178, 86, 192, 264, 65, 102, 98, 64, 107, 72, 86, 48, 175, 192, 16, 25, 361, 55, 41, 122, 20, 123, 24, 146, 144, 73, 94, 90, 60, 40, 36, 38, 32, 10, 20]
classes=['Cycling Road', 'Artistic Gymnastics', 'Rowing', 'Basketball', 'Handball', 'Karate', 'Wrestling', 'Rhythmic Gymnastics', 'Baseball/Softball', 'Athletics', 'Artistic Swimming', 'Shooting', 'Table Tennis', 'Judo', 'Football', 'Taekwondo', 'Boxing', 'Weightlifting', 'Archery', 'Fencing', 'Diving', 'Badminton', 'Beach Volleyball', 'Sailing', 'Hockey', 'Trampoline Gymnastics', 'Marathon Swimming', 'Swimming', 'Triathlon', 'Canoe Slalom', 'Water Polo', 'Surfing', 'Canoe Sprint', 'Cycling BMX Racing', 'Rugby Sevens', 'Volleyball', 'Equestrian', 'Tennis', 'Cycling Track', 'Golf', 'Skateboarding', 'Modern Pentathlon', 'Cycling Mountain Bike', '3x3 Basketball', 'Cycling BMX Freestyle', 'Sport Climbing']

# set colors and legends

N=[8,8,8,8,8,6]# number of colors  to extract from each cmap, sum(N)=len(classes)
base_cmaps = ['Greys','Purples','Reds','Blues','Oranges','Greens']

n_base = len(base_cmaps)

colors = np.concatenate([plt.get_cmap(name)(np.linspace(0.2,0.8,N[i])) for i,name in zip(range(n_base),base_cmaps)])
cmap = ListedColormap(colors)

gradient = np.linspace(0, 1, len(classes))
gradient = np.vstack((gradient, gradient))

indexes = []
for i in range(len(classes)):
    indexes.append(i)


scatter = plt.scatter(y=list_male_attendance, x=list_female_attendance, c=range(len(classes)), cmap=cmap, vmin=0, vmax=len(classes))


cbar = plt.colorbar(scatter)
cbar.ax.get_yaxis().set_ticks(np.array(indexes)+0.5)
cbar.ax.get_yaxis().set_ticklabels(classes)
cbar.ax.get_yaxis().set_ticklabels(classes)
cbar.ax.tick_params(labelsize=8) 

plt.ylabel("male attendance")
plt.xlabel("female attendance")
plt.tight_layout()
plt.show()

输出给出:

【讨论】:

  • 我们怎样才能增加这个图的宽度,因为它太杂乱了!
  • 通常,我建议使用 plt.xlimplt.ylim 将您的 xy 轴限制为它们的 minmax 使用,但看起来您的值接近1000 在两个轴上。相反,您可以尝试使用对数轴,看看它是否不那么混乱。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-24
  • 2014-09-13
  • 1970-01-01
  • 2019-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多