question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support adjusting the element size of legend

See original GitHub issue

Description

Sometimes, the plotted scatter size is small or big, we need to adjust the size in the legend to make it easier to read.

Steps to reproduce

import proplot as pplt
import numpy as np

def rand_data():
    return np.random.uniform(low=0., high=1., size=(100,))

# Generate data.
x1, y1 = [rand_data() for i in range(2)]
x2, y2 = [rand_data() for i in range(2)]

fig, axs = pplt.subplots()
s1 = axs.scatter(x1, y1, marker='o', label='first', s=2, c='b')
s2 = axs.scatter(x2, y2, marker='o', label='second', s=3, c='r')
axs.legend([s1, s2], loc='r', ncols=1)

Expected behavior: Support size=() in axs.legend().

Actual behavior: image

I have to adjust the scatter size in the legend manually:

legend = axs.legend([s1, s2], loc='r', ncols=1)

legend.legendHandles[0]._sizes = [30]
legend.legendHandles[1]._sizes = [30]

image

Equivalent steps in matplotlib

import matplotlib.pyplot as plt
import numpy as np

def rand_data():
    return np.random.uniform(low=0., high=1., size=(100,))

# Generate data.
x1, y1 = [rand_data() for i in range(2)]
x2, y2 = [rand_data() for i in range(2)]

plt.figure()
plt.scatter(x1, y1, marker='o', label='first', s=2., c='b')
plt.scatter(x2, y2, marker='o', label='second', s=3., c='r')
# Plot legend.
lgnd = plt.legend(loc="lower right", scatterpoints=1, fontsize=10)
lgnd.legendHandles[0]._sizes = [30]
lgnd.legendHandles[1]._sizes = [30]

Proplot version

0.7.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
lukelbdcommented, Aug 23, 2021

Yeah noticed that too… in your example it looks like you’re doing the equivalent of Collection.set_sizes() on the marker collection. In my example I’m doing the equivalent of Collection.set_linewidth() which (evidently) has some weird side-effects for this type of marker. I’ve added a 2-line workaround since this is probably a really common use-case for the “on-the-fly handle modification” feature. Now passing markersize=N will update scatter marker sizes without the weird side effect – passing markersize updates the collection with set_sizes().

You can install the dev branch for this example to work: pip install git+https://github.com/lukelbd/proplot.git.

import proplot as pplt
import numpy as np

def rand_data():
    return np.random.uniform(low=0., high=1., size=(100,))

# Generate data.
x1, y1 = [rand_data() for i in range(2)]
x2, y2 = [rand_data() for i in range(2)]

fig, axs = pplt.subplots()
s1 = axs.scatter(x1, y1, marker='o', label='first', s=2, c='b')
s2 = axs.scatter(x2, y2, marker='o', label='second', s=3, c='r')
# axs.legend([s1, s2], loc='r', ncols=1)
leg = axs.legend([s1, s2], loc='r', ncols=1, markersize=40)

test

1reaction
zxdawncommented, Aug 23, 2021

Got it! Maybe, it’s better to add the example to the legend section? (many matplotlib questions on StackOverflow discussed something like this).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Legend fitting strategies—ArcGIS Pro | Documentation
The Adjust columns fitting strategy automatically changes the number of columns to fit all items in the legend. The font sizes set in...
Read more >
Problem: Unable to resize the legend in ArcGIS Pro
In the Element pane, click the Placement tab. Under Size, change the Width and Height of the legend item to the preferred size....
Read more >
How to adjust the size of matplotlib legend box - Stack Overflow
To control the padding inside the legend (effectively making the legend box bigger) use the borderpad kwarg. For example, here's the default ...
Read more >
The Field Set Legend element - HTML - MDN Web Docs
Tip: you can click/tap on a cell for more information. Full support: Full support. Deprecated. Not for use in new websites.
Read more >
How to Change Legend Font Size in Matplotlib?
This example changes the font size of items in the legend. The font size parameter can have integer or float values. It also...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found