1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > python 散点图点击链接图片_Python散点图。 标记的大小和样式

python 散点图点击链接图片_Python散点图。 标记的大小和样式

时间:2021-05-27 21:38:06

相关推荐

python 散点图点击链接图片_Python散点图。 标记的大小和样式

我有一组要显示为散点图的数据。 我希望将每个点绘制为大小dx的正方形。

1

2

3

4

5

6x = [0.5,0.1,0.3]

y = [0.2,0.7,0.8]

z = [10.,15.,12.]

dx = [0.05,0.2,0.1]

scatter(x,y,c=z,s=dx,marker='s')

问题是散布函数读取的大小s以点^ 2为单位。 我想要的是让每个点都由面积dx ^ 2的正方形表示,该面积以"真实"单位(绘图单位)表示。 希望您能明白这一点。

我还有另一个问题。 散点图功能用黑色边框绘制标记,如何删除该选项并且完全没有边框?

从用户数据坐标系转换为显示坐标系。

并使用edgecolors ='none'绘制没有轮廓的面。

1

2

3

4

5

6import numpy as np

fig = figure()

ax = fig.add_subplot(111)

dx_in_points = np.diff(ax.transData.transform(zip([0]*len(dx), dx)))

scatter(x,y,c=z,s=dx_in_points**2,marker='s', edgecolors='none')

这不会按照OP的要求以绘图单位绘制正方形,但是不会调整大小的固定大小的正方形(例如,通过手动更改图形框大小)。

这可能是一个愚蠢的问题。但是,如果dx不是数组而是每个点(x,y,z)都相同,那么如何更改上面的代码。此外,我真的需要使用add_subplot吗?

您如何找到edgecolors参数?

如果您希望标记的大小与图形大小相同,则可以使用补丁:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18from matplotlib import pyplot as plt

from matplotlib.patches import Rectangle

x = [0.5, 0.1, 0.3]

y = [0.2 ,0.7, 0.8]

z = [10, 15, 12]

dx = [0.05, 0.2, 0.1]

cmap = plt.cm.hot

fig = plt.figure()

ax = fig.add_subplot(111, aspect='equal')

for x, y, c, h in zip(x, y, z, dx):

ax.add_artist(Rectangle(xy=(x, y),

color=cmap(c**2), # I did c**2 to get nice colors from your numbers

width=h, height=h)) # Gives a square of area h*h

plt.show()

注意:

正方形不在(x,y)的中心。 x,y实际上是

左下角的正方形。我以这种方式简化了我的代码。您

应该使用(x + dx/2, y + dx/2)。

颜色是从热色图中获取的。我用z ** 2给出颜色。

您还应该根据自己的需要进行调整

最后是您的第二个问题。您可以使用关键字参数edgecolor或edgecolors来获得散点标记的边框。它们分别是matplotlib颜色参数或rgba元组序列。如果将参数设置为"无",则不会绘制边框。

我认为我们可以通过添加补丁来做得更好。

根据文件:

This (PatchCollection) makes it easier to assign a color map to a heterogeneous

collection of patches.

This also may improve plotting speed, since PatchCollection will

draw faster than a large number of patches.

假设您要以数据单位绘制具有给定半径的圆的散布:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67def circles(x, y, s, c='b', vmin=None, vmax=None, **kwargs):

"""

Make a scatter of circles plot of x vs y, where x and y are sequence

like objects of the same lengths. The size of circles are in data scale.

Parameters

----------

x,y : scalar or array_like, shape (n, )

Input data

s : scalar or array_like, shape (n, )

Radius of circle in data unit.

c : color or sequence of color, optional, default : 'b'

`c` can be a single color format string, or a sequence of color

specifications of length `N`, or a sequence of `N` numbers to be

mapped to colors using the `cmap` and `norm` specified via kwargs.

Note that `c` should not be a single numeric RGB or RGBA sequence

because that is indistinguishable from an array of values

to be colormapped. (If you insist, use `color` instead.)

`c` can be a 2-D array in which the rows are RGB or RGBA, however.

vmin, vmax : scalar, optional, default: None

`vmin` and `vmax` are used in conjunction with `norm` to normalize

luminance data. If either are `None`, the min and max of the

color array is used.

kwargs : `~matplotlib.collections.Collection` properties

Eg. alpha, edgecolor(ec), facecolor(fc), linewidth(lw), linestyle(ls),

norm, cmap, transform, etc.

Returns

-------

paths : `~matplotlib.collections.PathCollection`

Examples

--------

a = np.arange(11)

circles(a, a, a*0.2, c=a, alpha=0.5, edgecolor='none')

plt.colorbar()

License

--------

This code is under [The BSD 3-Clause License]

(/licenses/BSD-3-Clause)

"""

import numpy as np

import matplotlib.pyplot as plt

from matplotlib.patches import Circle

from matplotlib.collections import PatchCollection

if np.isscalar(c):

kwargs.setdefault('color', c)

c = None

if 'fc' in kwargs: kwargs.setdefault('facecolor', kwargs.pop('fc'))

if 'ec' in kwargs: kwargs.setdefault('edgecolor', kwargs.pop('ec'))

if 'ls' in kwargs: kwargs.setdefault('linestyle', kwargs.pop('ls'))

if 'lw' in kwargs: kwargs.setdefault('linewidth', kwargs.pop('lw'))

patches = [Circle((x_, y_), s_) for x_, y_, s_ in np.broadcast(x, y, s)]

collection = PatchCollection(patches, **kwargs)

if c is not None:

collection.set_array(np.asarray(c))

collection.set_clim(vmin, vmax)

ax = plt.gca()

ax.add_collection(collection)

ax.autoscale_view()

if c is not None:

plt.sci(collection)

return collection

scatter函数的所有参数和关键字(marker除外)将以相似的方式工作。

我写了一个要点,包括圆形,椭圆形和正方形/矩形。如果您想要其他形状的集合,则可以自己修改。

如果要绘制颜色条,只需运行colorbar()或将返回的收集对象传递给colorbar函数。

一个例子:

1

2

3

4

5

6

7

8

9

10

11

12

13

14from pylab import *

figure(figsize=(6,4))

ax = subplot(aspect='equal')

#plot a set of circle

a = arange(11)

out = circles(a, a, a*0.2, c=a, alpha=0.5, ec='none')

colorbar()

#plot one circle (the lower-right one)

circles(1, 0, 0.4, 'r', ls='--', lw=5, fc='none', transform=ax.transAxes)

xlim(0,10)

ylim(0,10)

输出:

我想在开源项目中使用您的函数,但不能这样做,因为默认情况下,所有SO代码均受CC BY-SA许可。您可以明确声明代码的许可,最好是类似BSD的许可吗?

@neo很高兴知道这一点。我不熟悉许可证,我认为应该与matplotlib保持一致,因为我只是基于scatter函数编写了此代码。所以应该是PSF或其他?

您的代码段不是matplotlib的派生作品,因此您可以在任何许可下许可代码。我只会使用BSD 3子句,它在Python世界中很常见。

@neo那很好。不适使用BSD 3句。

为了使此Python 3兼容,我添加了以下代码片段

1

2

3

4try:

basestring

except NameError:

basestring = str

如何检查变量是否为具有python 2和3兼容性的字符串

这是必需的,因为basestring在Python 3中不可用。在Python 2中,basestring的目的是同时包含str和unicode。在Python 3中,str和unicode之间没有区别,只是str。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。