legend_elements ( ** kw ), loc = "lower right", title = "Price" ) plt. cmap ( 0.7 ), fmt = "$ ", func = lambda s : np. 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. 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). I draw a plot with markers like so: plt.scatter(xs1, ys1, s12, linewidths0, marker'o', label'hello') plt.scatter(xs2, ys2, s12, linewidths0, marker'x', label'hello') this produces the right size in the graph, but the symbols are hardly intelligible in the legend. legend_elements ( num = 5 ), loc = "upper left", title = "Ranking" ) ax. I am looking for a way to include a (matplotlib) legend that describe the size of points in a scatter plot, as this could be related to another variable, like in this basic example: import numpy as np import matplotlib. Even though there are 40 different # rankings, we only want to show 5 of them in the legend. 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. But now you can use everything scatter offers.Volume = np. The following also demonstrates how transparency of the markers can be adjusted by giving alpha a value between 0 and 1. ![]() No need to touch the source, even though this is quite a hack. To create a scatter plot with a legend one may use a loop and create one scatter plot per item to appear in the legend and set the label accordingly. Using s3t changes the raw values and thus the labels in the legend. What is neccessary is keeping the labels in the legend the way they are right now (25, 50, 75) while changing the size of the markers. ![]() It is a scalar or an array of the same length as x and y. Now the _sizes (another underscore property) does the trick. import matplotlib.pyplot as plt from numpy import pi n 16 create a n x n square with a marker at each point xdata ydata for x in range(n): for y in range(n): xdata.append(x) ydata.append(y) fig,ax plt.subplots(figsize7,7) important part: calculate the marker size so that the markers touch radius in data coordinates. Yes, the markers in the plot and the legend need to have the same size, and this could be changed by using s3t (in my example). (x, y, s20, c'b', marker'o', cmapNone, normNone, vminNone, vmaxNone, alphaNone, linewidthsNone, facetedTrue, vertsNone, holdNone, kwargs) The marker size. Lgnd = plt.legend(loc="lower left", scatterpoints=1, fontsize=10) Now I want to add a legend showing the red dots represent 'No view' and the blue dots represent 'View'. I have created a scatter plot of 'price' against 'size' but I colour coded the dots according to the 'view' column which contains only 0s and 1s. I have a 3-column table (price, size and view). Plt.scatter(x1, y1, marker='o', label='first', s=20., c='b') I am working in Python 3 Jupyter Notebook. A better hack: import matplotlib.pyplot as plt It may break down at any update in matplotlib. the marker size changed manually to be 6 points for both markers in the legendĪs you can see, this utilizes hidden underscore properties ( _legmarker) and is bug-ugly.scatter changed into a plot, which changes the marker scaling (hence the sqrt) and makes it impossible to use changing marker size (if that was intended).#change the marker size manually for both lines Lgnd = plt.legend(loc="lower left", numpoints=1, fontsize=10) However, I have a hack which does probably what you want: import matplotlib.pyplot as plt The scatter plots are especially challenging in this respect. Neither of these is very much fun, though #1 seems to be easier. The transform (scaling) has to take the original size into account. Add a transform into the PathCollection objects representing the dots in the image.import matplotlib.pyplot as plt from matplotlib. It is especially difficult with scatter plots ( wrong: see the update below). You may use the legends handlermap to set the desired marker size to the legend handles. ![]() ![]() Bad news is that there does not seem to be any simple way of setting equal sizes of points in the legend. I had a look into the source code of matplotlib.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |