How to make two markers share the same label in the legend

Note that in recent versions of matplotlib you can achieve this using class matplotlib.legend_handler.HandlerTuple as illustrated in this answer and also in this guide: import matplotlib.pyplot as plt from matplotlib.legend_handler import HandlerTuple fig, ax1 = plt.subplots(1, 1) # First plot: two legend keys for a single entry p2, = ax1.plot([3, 4], [2, 3], ‘o’, mfc=”white”, … Read more

Matplotlib automatic legend outside plot [duplicate]

EDIT: I HIGHLY RECOMMEND USING THE ANSWER FROM ImportanceOfBeingErnest: How to put the legend outside the plot EDIT END This one is easier to understand: import matplotlib.pyplot as plt x = [1,2,3] plt.subplot(211) plt.plot(x, label=”test1″) plt.plot([3,2,1], label=”test2″) plt.legend(bbox_to_anchor=(0, 1), loc=”upper left”, ncol=1) plt.show() now play with the to coordinates (x,y). For loc you can use: … Read more

How do I assign multiple legend labels at once?

You can iterate over your line objects list, so labels are individually assigned. An example with the built-in python iter function: lineObjects = plt.plot(x, y) plt.legend(iter(lineObjects), (‘foo’, ‘bar’, ‘baz’))` Edit: after updating to matplotlib 1.1.1, it looks like the plt.plot(x, y), with y as a list of lists (as provided by the author of the … Read more

How to move or position a legend in ggplot2

In versions > 0.9.3 (when opts was deprecated) theme(legend.position = “bottom”) Older version: Unfortunately it’s a bug in ggplot2 which I really really hope to fix this summer. Update: The bug involving opts(legend.position = “left”) has been fixed using the most current version of ggplot2. In addition, version 0.9.0 saw the introduction of guide_legend and … Read more