Matplotlib Scatter Plot Size Legend
Downvote this answer if you find it unclear or not useful.
Matplotlib scatter plot size legend. Add artist legend1 produce a legend with a cross section of sizes from the scatter handles labels scatter. Each element in the x y and classes lists corresponds to one point in the plot. Using the pyplot function legend. Handle set sizes 6 0 with matplotlib 2 0 0.
The size of datapoints within a matplotlib scatterplot are determined by an optional variable s. Set the same marker size of all points import numpy as np import matplotlib pyplot as plt x 1 2 3 4 5 y np sin x plt scatter x y s 500 c magenta plt title scatter plot of sinx plt xlabel x plt ylabel sinx plt xlim 0 6 plt ylim 2 2 plt show. Show activity on this post. The marker size in points 2.
The number of marker points in the legend when creating a legend entry for a pathcollection scatter plot. Legend elements prop sizes. N 45 x y np. The docs define s as.
Similarly to the answer assuming you want all the markers with the same size. Scatteryoffsets iterable of floats default. Lgnd plt legend loc lower left scatterpoints 1 fontsize 10 for handle in lgnd legendhandles. According to documentation s is the marker size in points 2.
Subplots scatter ax. Create legend with bubble size using numpy and matplotlib legend with bubble size import numpy as np import matplotlib pyplot as plt import pandas as pd n 50 m 5 number of bins x np random rand n y np random rand n a2 400 np random rand n create the dataframe from your randomised data and bin it using groupby. Legend elements loc lower left title classes ax. I want to create a matplotlib scatter plot with a legend showing the colour for each class.
Scatter x y c c s s produce a legend with the unique colors from the scatter legend1 ax. Matplotlib scatter marker size scalar in plt scatter the default marker size is s 72. Rand 2 n c np. 0 0 is at the base the legend text and 1 0 is at the top.
How to add a legend for a scatter plot in matplotlib. 0 375 0 5 0 3125 the vertical offset relative to the font size for the markers created for a scatter plot legend entry. Randint 10 220 size n fig ax plt. For example i have a list of x and y values and a list of classes values.
Conversely if you want your data points to be smaller than normal set s to be less than 20. I want each class to have its own colour which i have already coded but then i want the classes to be displayed in a legend.