Graduated size symbols in legends

佐手、 提交于 2019-12-07 12:12:56

问题


I have plotted a bubble chart with the circles' sizes corresponding to a list of values using matplotlib. However, I'm having trouble creating a legend for the plot that has variable size symbols that corresponds to the listed size. Such as the one in the link above.

Is there a way to create this legend in matplotlib without manually drawing circles and text on the plot? Thanks in advance for your time and thoughts!

Cindy


回答1:


Yes, you can be specific about which data you make a legend for (choosing illustrative sizes) and specify their labels in the legend.

Starting with data from one of the other scatter-legend questions here, and leaving some in that doesn't go into the legend (crosses):

import matplotlib.pyplot as plt
from numpy.random import random

colors = ['b', 'c', 'y', 'm', 'r']

ll = plt.scatter(random(10), random(10), s=10, marker='o', color=colors[0])
l  = plt.scatter(random(10), random(10), s=20, marker='o', color=colors[1])
a  = plt.scatter(random(10), random(10), s = 300, marker='o', color=colors[2])
z  = plt.scatter(random(10), random(10), s = 35, marker='+', color=colors[3]) # not in legend

plt.legend((ll, l, a),
       ('10', '20', '300'),
       scatterpoints=1,
       loc='lower left',
       ncol=1,
       fontsize=8)

plt.show()

Left to do: moving the legend outside your plot. (And it looks like you might need to add invisible vertical spacers.)



来源:https://stackoverflow.com/questions/19937375/graduated-size-symbols-in-legends

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!