![]() legend_elements ( ** kw ), loc = "lower right", title = "Price" ) plt. The primary difference of plt.scatter from plt.plot is that it can be used to create scatter plots where the properties of each individual point (size, face color, edge color, etc. y plt.scatter(x, y, label'Original Data', color'steelblue') add legend plt.legend() display plot plt. cmap ( 0.7 ), fmt = "$ ", func = lambda s : np. The following code shows how to create a scatter plot in matplotlib with a default legend: import matplotlib.pyplot as plt define data to plot x 1, 2, 3, 4, 5, 6, 7 y 2, 3, 5, 8, 12, 18, 27 create scatter plot of x vs. kw = dict ( prop = "sizes", num = 5, color = scatter. Note how we target at 5 elements here, but obtain only 4 in the # created legend due to the automatic round prices that are chosen for us. You can use the following syntax to add a legend to a scatterplot in Matplotlib: import matplotlib.pyplot as plt from lors import ListedColormap define values, classes, and colors to map values 0, 0, 1, 2, 2, 2 classes 'A', 'B', 'C' colors ListedColormap ( 'red', 'blue', 'purple') create scatterplot scatter plt. The *fmt* ensures to show the price # in dollars. Because we want to show the prices # in dollars, we use the *func* argument to supply the inverse of the function # used to calculate the sizes from above. ![]() add_artist ( legend1 ) # Produce a legend for the price (sizes). legend_elements ( num = 5 ), loc = "upper left", title = "Ranking" ) ax. Syntax: ( title1, Title2, ncol 1, loc upper left ,bboxtoanchor (1, 1) ) Parameters : ncol: takes int, optional parameter the default value is 1. Instead of creating a patch of color we could have created a line with a marker: import matplotlib. Even though there are 40 different # rankings, we only want to show 5 of them in the legend. We will use the () method to describe and label the elements of the graph and distinguishing different plots from the same graph. scatter ( volume, amount, c = ranking, s = 0.3 * ( price * 3 ) ** 2, vmin =- 3, vmax = 3, cmap = "Spectral" ) # Produce a legend for the ranking (colors). ![]() subplots () # Because the price is much too small when being provided as size for ``s``, # we normalize it to some useful point sizes, s=0.3*(price*3)**2 scatter = ax. ![]() uniform ( 1, 10, size = 40 ) fig, ax = plt. ![]()
0 Comments
Leave a Reply. |