There is a reason, however, that the size of markers is defined in this way. Because of the scaling of area as the square of width, doubling the width actually appears to increase the size by more than a factor 2 (in fact it increases it by a factor of 4). To see this consider the following two examples and the output they produce.
# doubling the width of markers x = [0, 2, 4, 6, 8, 10] y = [0] * len(x) s = [20 * 4 ** n for n in range(len(x)) ] plt.scatter(x, y, s = s) plt.show()
Notice how the size increases very quickly. If instead we have
# doubling the area of markers x = [0, 2, 4, 6, 8, 10] y = [0] * len(x) s = [20 * 2 ** n for n in range(len(x)) ] plt.scatter(x, y, s = s) plt.show()
x = [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
s_exp = [20 * 2 ** n
for n in range(len(x))
]
s_square = [20 * n ** 2
for n in range(len(x))
]
s_linear = [20 * n
for n in range(len(x))
]
plt.scatter(x, [1] * len(x), s = s_exp, label = '$s=2^n$', lw = 1)
plt.scatter(x, [0] * len(x), s = s_square, label = '$s=n^2$')
plt.scatter(x, [-1] * len(x), s = s_linear, label = '$s=n$')
plt.ylim(-1.5, 1.5)
plt.legend(loc = 'center left', bbox_to_anchor = (1.1, 0.5), labelspacing = 3)
plt.show()
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([0], [0], marker = "o", markersize = 10)
ax.plot([0.07, 0.93], [0, 0], linewidth = 10)
ax.scatter([1], [0], s = 100)
ax.plot([0], [1], marker = "o", markersize = 22)
ax.plot([0.14, 0.86], [1, 1], linewidth = 22)
ax.scatter([1], [1], s = 22 ** 2)
plt.show()
It might be useful to be able to specify sizes in pixels instead of points. If the figure dpi is 72 as well, one point is one pixel. If the figure dpi is different (matplotlib default is fig.dpi=100
),
1 point == fig.dpi / 72. pixels
import matplotlib.pyplot as plt
for dpi in [72, 100, 144]:
fig, ax = plt.subplots(figsize = (1.5, 2), dpi = dpi)
ax.set_title("fig.dpi={}".format(dpi))
ax.set_ylim(-3, 3)
ax.set_xlim(-2, 2)
ax.scatter([0], [1], s = 10 ** 2,
marker = "s", linewidth = 0, label = "100 points^2")
ax.scatter([1], [1], s = (10 * 72. / fig.dpi) ** 2,
marker = "s", linewidth = 0, label = "100 pixels^2")
ax.legend(loc = 8, framealpha = 1, fontsize = 8)
fig.savefig("fig{}.png".format(dpi), bbox_inches = "tight")
plt.show()
You can use markersize to specify the size of the circle in plot method
import numpy as np import matplotlib.pyplot as plt x1 = np.random.randn(20) x2 = np.random.randn(20) plt.figure(1) # you can specify the marker size two ways directly: plt.plot(x1, 'bo', markersize = 20) # blue circle with size 10 plt.plot(x2, 'ro', ms = 10, ) # ms is just an alias for markersize plt.show()
It is the area of the marker. I mean if you have s1 = 1000
and then s2 = 4000
, the relation between the radius of each circle is: r_s2 = 2 * r_s1
. See the following plot:
plt.scatter(2, 1, s = 4000, c = 'r')
plt.scatter(2, 1, s = 1000, c = 'b')
plt.scatter(2, 1, s = 10, c = 'g')
I also attempted to use 'scatter' initially for this purpose. After quite a bit of wasted time - I settled on the following solution.
import matplotlib.pyplot as plt
input_list = [{
'x': 100,
'y': 200,
'radius': 50,
'color': (0.1, 0.2, 0.3)
}]
output_list = []
for point in input_list:
output_list.append(plt.Circle((point['x'], point['y']), point['radius'], color = point['color'], fill = False))
ax = plt.gca(aspect = 'equal')
ax.cla()
ax.set_xlim((0, 1000))
ax.set_ylim((0, 1000))
for circle in output_list:
ax.add_artist(circle)
In this Matplotlib Tutorial, we learned how to set size for markers in Scatter Plot.,To set specific size for markers in Scatter Plot in Matplotlib, pass required sizes for markers as list, to s parameter of scatter() function, where each size is applied to respective data point.,www.tutorialkart.com - ©Copyright - TutorialKart 2021,In the following example, we will draw a scatter plot with 6 (six) data points, and set specific size for the markers of these data points on the Scatter plot, with a list of numbers. Each number in the list is the size of the marker in Scatter plot.
The following is definition of scatter()
function with s
parameter, at third position, whose default value is None
.
matplotlib.pyplot.scatter(x, y, s = None, c = None, marker = None, cmap = None, norm = None, vmin = None, vmax = None, alpha = None, linewidths = None, *, edgecolors = None, plotnonfinite = False, data = None, ** kwargs)
example.py
import matplotlib.pyplot as plt #data x = [0, 1, 1, 2, 2, 3] y = [0, 1, 2, 1, 2, 3] #markers ' size size = [40, 300, 125, 180, 72, 60] #scatter plot plt.scatter(x, y, s = size) plt.show()
You can specify the marker size with the parameter s and the marker color with c in the plt.scatter() function.
You can specify the marker size with the parameter s and the marker color with c in the plt.scatter()
function.
import matplotlib.pyplot as plt import matplotlib.colors # Prepare a list of integers val = [2, 3, 6, 9, 14] # Prepare a list of sizes that increases with values in val sizevalues = [i ** 2 * 50 + 50 for i in val ] # Prepare a list of colors plotcolor = ['red', 'orange', 'yellow', 'green', 'blue'] # Draw a scatter plot of val points with sizes in sizevalues and # colors in plotcolor plt.scatter(val, val, s = sizevalues, c = plotcolor) # Set axis limits to show the markers completely plt.xlim(0, 20) plt.ylim(0, 20) plt.show()