[英]Set variable point size in matplotlib
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]
) 第1列和第2列存储在data[0]
而data[1]
则是(x,y)
值,我希望每个点都具有相对于第3列的大小(即: data[2]
)
help(plt.plot)
shows help(plt.plot)
显示
markersize or ms: float
so it appears plt.plot
does not allow the markersize to be an array. 因此看来plt.plot
不允许markersize为数组。
You could use plt.scatter
however: 您可以使用plt.scatter
但是:
ax1.scatter(data[0], data[1], marker='o', c='b', s=data[2], label='the data')
PS. PS。 You can also verify that plt.plot
's markersize
must be a float by searching for "markersize" in the official documentation . 您还可以通过在官方文档中搜索“ markersize”来验证plt.plot
的markersize
必须为float。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.