1

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!

  • 1
    `title_fontsize` is only available in matplotlib 3.0.0. If you are on a version lower than that you can use one of the options from this question - https://stackoverflow.com/questions/12402561/how-to-set-font-size-of-matplotlib-axis-legend – DavidG Oct 05 '18 at 10:11
  • 1
    Not sure about the extra legend entry. Are you able to provide the csv file, or some fake data which reproduces this? – DavidG Oct 05 '18 at 10:21
  • You can choose which legends to show if you just want to get rid of this extra legend entry. – Sheldore Oct 05 '18 at 10:26

1 Answers1

1

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=""
Diziet Asahi
  • 38,379
  • 7
  • 60
  • 75