如何在Matplotlib散点图中添加第二个键?

时间:2016-04-08 19:23:55

标签: python matplotlib legend scatter-plot

我有一个Matplotlib散点图,其中点标记因属性,形状和颜色而异,具体取决于与点关联的两个属性的值("净大小"和"批量大小& #34;分别),但使用legend我似乎只能获得第一个属性的键:

enter image description here

如何为颜色添加第二个键(作为"批量大小"的函数)?事实上,我也喜欢现有的钥匙只是区分形状(没有颜色),如果可以实现的话。我也很乐意用一些(可能更具信息性)的灰度或光谱替换颜色。

import matplotlib.pyplot as plt

batch_color_specs = ['r', 'm', 'b', 'y', 'g', 'k']
batch_sizes = [10, 20, 40, 80, 100, 200]

# Data below
fig, ax = plt.subplots()
for net_size, net_size_mark, net_size_alpha in zip([30, 60, 100], ['s', 'o', '^'], [0.3, 1.0, 0.3]) :
    ax.scatter([plateau[str(net_size)][str(bs)][1] for bs in batch_sizes], 
               [plateau[str(net_size)][str(bs)][2] for bs in batch_sizes],
               c=batch_color_specs, marker=net_size_mark, s=50, edgecolors='none', alpha=net_size_alpha,
               label=str(net_size))
plt.xlabel('time (s)')
plt.ylabel('accuracy (%)')
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0., frameon=False, title='Net Size')
plt.show()
plateau = {}
plateau['30'] = {
    '10':  (80.3961 ,  45.0351 ,  96.1100,   20),
    '20': (132.0082 ,  57.7471 ,  96.4600,   42),
    '40': (129.4380 ,  44.1056 ,  96.1500,   49),
    '80': (120.6937 ,  35.7472 ,  95.5400,   49),
    '100': (173.8619 ,  48.9901 ,  95.8900,   71),
    '200': (191.5136 ,  57.8109 ,  95.2600,   76)}
plateau['60'] = {
     '10': (133.2443 ,  80.0031 ,  97.1900,   25),
     '20': (165.0099 ,  77.2276 ,  97.2400,   41),
     '40': (205.8393 ,  78.1126 ,  96.9700,   60),
     '80': (154.8232 ,  49.9324 ,  96.7100,   49),
    '100': (222.3961 ,  68.9063 ,  97.0500,   72),
    '200': (290.0711 ,  88.5366 ,  96.2300,   94)}
plateau['100'] = {
     '10': (177.3463 , 111.3308 ,  97.7300,   24),
     '20': (234.5564 , 117.5112 ,  97.7900,   42),
     '40': (162.1202 ,  65.7477 ,  97.3500,   35),
     '80': (247.8064 ,  87.5982 ,  97.3800,   59),
    '100': (243.6714 ,  82.2312 ,  97.2100,   60),
    '200': (420.6481 , 139.7030 ,  96.9800,  105)}

0 个答案:

没有答案