Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change marker size/scale in legend when marker is set to pixel

I am scatter ploting data points with a very small marker (see screengrab below). When I use the very small marker ',' the legend is very hard to read (example code taken from here).
(Python 3, Jupyter lab)

enter image description here

How can I increase the size of the marker in the legend. The two versions shown on the above mentioned site do not work:

legend = ax.legend(frameon=True)  
for legend_handle in legend.legendHandles:  
    legend_handle._legmarker.set_markersize(9)

and

ax.legend(markerscale=6)

The two solutions do however work when the marker is set to '.'.
How can I show bigger makers in the legend?

Sample Code from intoli.com:

import numpy as np
import matplotlib.pyplot as plt
np.random.seed(12)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

for i in range(5):
    mean = [np.random.random()*10, np.random.random()*10]
    covariance = [ [1 + np.random.random(), np.random.random() - 1],  [0, 1 + np.random.random()], ]
    covariance[1][0] = covariance[0][1]  # must be symmetric
    x, y = np.random.multivariate_normal(mean, covariance, 3000).T
    plt.plot(x, y, ',', label=f'Cluster {i + 1}')

ax.legend(markerscale=12)

fig.tight_layout()
plt.show()
like image 240
rul30 Avatar asked Jan 28 '19 08:01

rul30


People also ask

How do I increase the size of a scatter point in Matplotlib?

The points in the scatter plot are by default small if the optional parameters in the syntax are not used. The optional parameter 's' is used to increase the size of scatter points in matplotlib.

How do I make the legend lines thicker in Matplotlib?

We can change the line width (line thickness) of lines in Python Matplotlib legend by using the set_linewidth() method of the legend object and the setp() method of the artist objects.

What is S in scatter plot?

s: size in points^2. It is a scalar or an array of the same length as x and y.

How do I scale up the marker size in the legend?

The legend () function takes a markerscale parameter which allows you to scale up the marker sizes in the legend. Our original marker sizes were 1.5 so to scale them up to 9 we would want to scale them by a factor of 6 (because 1.5*6=9).

How do you plot a legend with markerscale in Python?

The legend() function takes a markerscale parameter which allows you to scale up the marker sizes in the legend. Our original marker sizes were 1.5 so to scale them up to 9 we would want to scale them by a factor of 6 (because 1.5*6=9). This allows us to simply plot our legend with.

How to adjust the size of marker in a plot?

plt.plot (data1, data2, marker=".") Example 2: To adjust the size of marker in a plot use markersize parameter to plot method to adjust the marker size. Here in this example, we adjusted the marker size to size 20 such that the maker size is increased from its standard size.

Is it possible to set marker size in legend in MATLAB?

No public field MarkerSize exists for class matlab.graphics.GraphicsPlaceholder. There is no MarkerSize property on the Text class. I am trying to set each marker size in the legend equal to the size in the figure.


Video Answer


1 Answers

You can get 1 pixel sized markers for a plot by setting the markersize to 1 pixel. This would look like

plt.plot(x, y, marker='s', markersize=72./fig.dpi, mec="None", ls="None")

What the above does is set the marker to a square, set the markersize to the ppi (points per inch) divided by dpi (dots per inch) == dots == pixels, and removes lines and edges.

Then the solution you tried using markerscale in the legend works nicely.

Complete example:

import numpy as np
import matplotlib.pyplot as plt
np.random.seed(12)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

for i in range(5):
    mean = [np.random.random()*10, np.random.random()*10]
    covariance = [ [1 + np.random.random(), np.random.random() - 1],  [0, 1 + np.random.random()], ]
    covariance[1][0] = covariance[0][1]  # must be symmetric
    x, y = np.random.multivariate_normal(mean, covariance, 3000).T
    plt.plot(x, y, marker='s', markersize=72./fig.dpi, mec="None", ls="None", 
             label=f'Cluster {i + 1}')

ax.legend(markerscale=12)

fig.tight_layout()
plt.show()

enter image description here

like image 200
ImportanceOfBeingErnest Avatar answered Sep 28 '22 17:09

ImportanceOfBeingErnest