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()

53BM4.png

注意:

正方形不在(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]

(http://opensource.org/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)

输出:

slpoC.png

我想在开源项目中使用您的函数,但不能这样做,因为默认情况下,所有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。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Python中使用散点图进行曲线拟合,可以使用SciPy库中的curve_fit函数。首先,我们需要导入所需的库和数据: ```python import numpy as np import matplotlib.pyplot as plt from scipy.optimize import curve_fit # 定义我们要拟合的函数 def func(x, a, b, c): return a * np.sin(b * x) + c # 假设我们有一些数据 x_data = np.linspace(0, 2 * np.pi, 50) y_data = 2.5 * np.sin(1.5 * x_data) + 1.5 + np.random.normal(size=50) ``` 在这里,我们定义了一个名为func的函数,它将被用作我们要拟合的函数。我们还创建了一些假数据,其中x_data是介于0到2π之间的50个等间距数据,y_data是根据这些x值计算出来的一些带有噪声的y值。 接下来,我们使用curve_fit函数进行拟合: ```python # 使用curve_fit进行拟合 popt, pcov = curve_fit(func, x_data, y_data) # 输出拟合的参数 print(popt) # 绘制原始数据和拟合的曲线 plt.scatter(x_data, y_data) plt.plot(x_data, func(x_data, *popt), 'r') plt.show() ``` 这里的curve_fit函数将我们的数据和func函数作为输入,并返回一个包含拟合参数的数组popt和一个协方差矩阵pcov。我们可以使用popt来绘制拟合的曲线。 最后,我们可以使用Matplotlib库来绘制散点图和拟合的曲线。在这里,我们首先使用plt.scatter函数绘制原始数据的散点图,然后使用plt.plot函数绘制拟合曲线。 运行完整的代码,你将会得到一个包含原始数据和拟合曲线的散点图

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值