Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing a Matplotlib Rectangle to Circle in a Legend

Consider the following plotting code:

plt.figure(figsize=(10,6))
for k in range(nruns):
    plt.plot(Thing1['Data'][:,k],color='Grey',alpha=0.10)
plt.plot(Thing2[:,1],Thing2[:,4],'ko')
a = plt.Rectangle((0, 0), 1, 1, fc="Grey",alpha=0.50)
b = plt.Rectangle((0, 0), 1, 1, fc="Black", alpha=1.00)
plt.legend([a,b], ["Thing1","Thing2"],loc=2,fontsize='small')
plt.xlabel("Time",fontsize=16)
plt.ylabel("Hijinks",fontsize=16)
plt.show()

I'd really like "b" to be a circle, rather than a rectangle. But I'm rather horrid at matplotlib code, and especially the use of proxy artists. Any chance there's a straightforward way to do this?

like image 926
Fomite Avatar asked Apr 09 '26 20:04

Fomite


1 Answers

You're very close. You just need to use a Line2D artist and set its properties like ususal:

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(10,6))

fakexy = (0, 0)
a = plt.Rectangle(fakexy, 1, 1, fc="Grey",alpha=0.50)
b = plt.Line2D(fakexy, fakexy, linestyle='none', marker='o', markerfacecolor="Black", alpha=1.00)

ax.legend([a, b], ["Thing1", "Thing2"], loc='upper left', fontsize='small')
ax.set_xlabel("Time", fontsize=16)
ax.set_ylabel("Hijinks", fontsize=16)

I get:

enter image description here

like image 67
Paul H Avatar answered Apr 13 '26 22:04

Paul H



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!