Python 在 matplotlib 中设置可变点大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16774197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:38:03  来源:igfitidea点击:

Set variable point size in matplotlib

pythonpython-2.7python-3.xmatplotlib

提问by Gabriel

I want to set a variablemarker 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]

I'm using the CanopyIDE by the way.

顺便说一下,我正在使用CanopyIDE。

采纳答案by unutbu

help(plt.plot)shows

help(plt.plot)显示

  markersize or ms: float         

so it appears plt.plotdoes not allow the markersize to be an array.

所以它似乎plt.plot不允许标记大小是一个数组。

You could use plt.scatterhowever:

plt.scatter但是,您可以使用:

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 markersizemust be a float by searching for "markersize" in the official documentation.

附注。您还可以通过在官方文档中搜索“markersize”来验证plt.plot'smarkersize必须是浮点数。