PleasureTheory420
PleasureTheory420

Reputation: 35

An issue with Matplotlib Legend

Here is my python code for a viz of train fares, time to London and time to a Stableyard. For some reason, there is an extra item labelled "ticket" within the plot legend. What is this and how do I get rid of it?

Also when I try and include the title_fontsize param, I get thrown:

__init__() got an unexpected keyword argument 'title_fontsize'

How would I go about changing the size of the legend title?

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

flat_data = pd.read_csv("C:/BLAHBALHBLAH/flats.csv")

X = flat_data["commute"]
Y = flat_data["ticket"]
size = 180 * flat_data["stables"]
names = list(flat_data["location"])

plt.figure(figsize=(20,15))
plt.scatter(X,Y,s=size, alpha=0.4,cmap="bone",c="r")
plt.xlabel("Time to London (mins)")
plt.ylabel("Cost of Season Ticket (£)")
plt.axis([32,80,1500,5200])
plt.grid(False)

for index, name in enumerate(names):
    plt.annotate(name, (X[index]-len(name)/6, Y[index]),size=15) 


for time in [7,36]:
    plt.scatter([], [], c='r', alpha=0.4, s=180*time, label=str(time) + ' mins')

plt.legend(frameon=False, labelspacing=4, title='Time to Stables', handletextpad =3,fontsize="x-large")

plt.show()

Plot

Thanks!

Upvotes: 1

Views: 4865

Answers (1)

Diziet Asahi
Diziet Asahi

Reputation: 40667

About your extra legend, you should suppress the legend entry from your first call to scatter():

plt.scatter(X,Y,s=size, alpha=0.4,cmap="bone",c="r", label=None)   # or label=""

Upvotes: 1

Related Questions