Set variable point size in matplotlib
Question:
I want to set a variable marker size in a scatter plot. This is my code:
import numpy as np
import matplotlib.pyplot as plt
from os import getcwd
from os.path import join, realpath, dirname
mypath = realpath(join(getcwd(), dirname(__file__)))
myfile = 'b34.dat'
data = np.loadtxt(join(mypath,myfile),
usecols=(1,2,3),
unpack=True)
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(data[0], data[1], 'bo', markersize=data[2], label='the data')
plt.show()
The file I’m importing has three columns. Columns 1 and 2 are stored in data[0]
and data[1]
) are the (x,y)
values and I want each point to have a size relative to column 3 (ie: data[2]
)
I’m using the Canopy IDE by the way.
Answers:
help(plt.plot)
shows
markersize or ms: float
so it appears plt.plot
does not allow the markersize to be an array.
You could use plt.scatter
however:
ax1.scatter(data[0], data[1], marker='o', c='b', s=data[2], label='the data')
PS. You can also verify that plt.plot
‘s markersize
must be a float by searching for “markersize” in the official documentation.
I want to set a variable marker size in a scatter plot. This is my code:
import numpy as np
import matplotlib.pyplot as plt
from os import getcwd
from os.path import join, realpath, dirname
mypath = realpath(join(getcwd(), dirname(__file__)))
myfile = 'b34.dat'
data = np.loadtxt(join(mypath,myfile),
usecols=(1,2,3),
unpack=True)
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(data[0], data[1], 'bo', markersize=data[2], label='the data')
plt.show()
The file I’m importing has three columns. Columns 1 and 2 are stored in data[0]
and data[1]
) are the (x,y)
values and I want each point to have a size relative to column 3 (ie: data[2]
)
I’m using the Canopy IDE by the way.
help(plt.plot)
shows
markersize or ms: float
so it appears plt.plot
does not allow the markersize to be an array.
You could use plt.scatter
however:
ax1.scatter(data[0], data[1], marker='o', c='b', s=data[2], label='the data')
PS. You can also verify that plt.plot
‘s markersize
must be a float by searching for “markersize” in the official documentation.