以相同的坐标绘制/分散位置和标记大小 [英] Plot/scatter position and marker size in the same coordinates

查看:170
本文介绍了以相同的坐标绘制/分散位置和标记大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以编程方式在pylab中显示各种类型的图表。 pylab的分散函数在其坐标轴的不同坐标中显示标记,但标记大小是以像素为单位的。这对于日常图形情节是有意义的。但是,我想在同一坐标中给出坐标和标记大小!例如下面的代码在(0.0,0.0)和(0.5,0.5)处显示两个大小为20的圆圈。这些应该几乎完全重叠,因为半径比中心之间的距离大得多。但是,它们并不是因为尺寸是以像素为单位的,而是以轴坐标表示的。

  import pylab 
pylab.scatter([0,0.5],[0,0.5],s = 20,c ='b',marker ='o')

有没有办法用相同的坐标给出散点图(或绘制任何形状,线条等)的大小和位置?我感兴趣的是一个矢量图形输出(pdf,svg等)。

解决方案 pylab。 scatter 函数根据点数^ 2中的大小获取值大小。这个大小将独立于坐标轴,散点图的本质也是如此(如果您在散点图中的某个区域上缩小点数,则大小无关紧要)。



如果你想说绘制给定大小的圆圈,你应该在pylab中使用circle命令

  import pylab 
axes = pylab.axes()
circle1 = pylab.Circle((0,0),radius = 20,alpha = .5)
circle2 = pylab.Circle((0.5,0.5),radius = 20,alpha = .5)
axes.add_patch(circle1)
axes.add_patch(circle2)
pylab.axis ('scaled')
pylab.show()

如果需要散点图你可以这样做:
$ b $ pre $ import pylab
import matplotlib

def my_circle_scatter (x_array,y_array):
circle = pylab.Circle((x,y),radius =(x_array,y_array,radius = 0.5,** kwargs)半径,** kwargs)
axes.add_patch(圆)
返回True

def my_square_scatter(axes,x_array,y_array,size = 0.5,** kwargs):
size = float(size)
for x,y in zip(x_array,y_array):
square = pylab.Rectangle((x-size / 2,y-size / 2),size,size,** kwargs)
axes.add_patch(square)
return True

def my_polygon_scatter(axes,x_array,y_array,resolution = 5,radius = 0.5,** kwargs):
'''分辨率是多边形的边数'''
在zip中为x,y (x_array,y_array):
polygon = matplotlib.patches.CirclePolygon((x,y),radius = radius,resolution = resolution,** kwargs)
axes.add_patch(polygon)
返回True

axes = pylab.axes()
my_circle_scatter(axes,[0,0.5],[0,0.5],radius = 2,alpha = .5,color ='b ')
my_square_scatter(axes,[-3,-4,2,3],[1,-3,0,3],size = .5,alpha = .5,color ='r')
my_polygon_scatter(轴,[-1,-2,3],[-3,0,3],半径= .5,分辨率= 3,alpha = .5,color ='g')
my_polygon_scatter(axes,[4,1.5,3],[2,-1,-3],radius = .5,resolution = 5,alpha = .5,color ='k')
pylab.axis 'scaled')
pylab.axis([ - 5,5,-5,5])
pylab.show()

请参阅 http://matplotlib.sourceforge.net /api/artist_api.html#module-matplotlib.patches 用于其他可绘制对象。



来自第一个片段的示例输出:



第二个片段的示例输出:
< img src =https://i.stack.imgur.com/aMdZZ.pngalt =matplotlib scatter plot>



编辑Griff:如果你想要为每个点处理不同的半径,你可以做一些简单的事情,比如:

  def my_circle_scatter_radii(axes,x_array,y_array,radii_array ,** kwargs):zip(x_array,y_array,radii_array)中
(x,y,r):
circle = pylab.Circle((x,y),radius = r,** kwargs)
axes.add_patch(circle)
return True

同时处理这两种情况是也很简单(例如,检查它的radii_array是否是一个int / float,如果是的话就把它作为一个数组)。


I am trying to programmatically display various types of diagrams in pylab. pylab's scatter function displays markers at different co-ordinates in temrs of its axis, but the marker sizes are in terms of pixels. This makes sense for everyday graph plots. However, I want to give the co-ordinates and marker sizes in the same co-ordinates! e.g., the following code displays two circles at (0.0,0.0) and (0.5,0.5) with sizes of 20 each. These should overlap almost completely as the radius is much bigger than the distance between the centers. However, they don't because the sizes are in pixels while the positions are in terms of the axis coordinates.

import pylab
pylab.scatter([0,0.5], [0,0.5], s=20, c='b', marker='o')

Is there a way to make a scatter plot (or draw any shapes, lines etc.) with the sizes and positions given in the same co-ordinates? I am interested in a vector graphics output (pdf, svg etc.).

解决方案

The pylab.scatter function takes a value size based on the size in points^2. This size will be independent of the axes, as is the nature of a scatter plot (it doesn't make sense if you narrow in on a region in a scatter plot that the points get bigger).

If you want to say draw circles that are a given size, you should use the circle command in pylab

import pylab
axes = pylab.axes()
circle1 = pylab.Circle((0,0), radius=20, alpha=.5)
circle2 = pylab.Circle((0.5,0.5), radius=20, alpha=.5)
axes.add_patch(circle1)
axes.add_patch(circle2)
pylab.axis('scaled')
pylab.show()

If you need something with scatter plot like ability, you could do something like

import pylab
import matplotlib

def my_circle_scatter(axes, x_array, y_array, radius=0.5, **kwargs):
    for x, y in zip(x_array, y_array):
        circle = pylab.Circle((x,y), radius=radius, **kwargs)
        axes.add_patch(circle)
    return True

def my_square_scatter(axes, x_array, y_array, size=0.5, **kwargs):
    size = float(size)
    for x, y in zip(x_array, y_array):
        square = pylab.Rectangle((x-size/2,y-size/2), size, size, **kwargs)
        axes.add_patch(square)
    return True

def my_polygon_scatter(axes, x_array, y_array, resolution=5, radius=0.5, **kwargs):
    ''' resolution is number of sides of polygon '''
    for x, y in zip(x_array, y_array):
        polygon = matplotlib.patches.CirclePolygon((x,y), radius=radius, resolution=resolution, **kwargs)
        axes.add_patch(polygon)
    return True

axes=pylab.axes()
my_circle_scatter(axes, [0,0.5], [0,0.5], radius=2, alpha=.5, color='b')
my_square_scatter(axes, [-3,-4,2,3], [1,-3,0,3], size=.5, alpha=.5, color='r')
my_polygon_scatter(axes, [-1,-2,3], [-3,0,3], radius=.5, resolution=3, alpha=.5, color='g')
my_polygon_scatter(axes, [4,1.5,3], [2,-1,-3], radius=.5, resolution=5, alpha=.5, color='k')
pylab.axis('scaled')
pylab.axis([-5,5,-5,5])
pylab.show()

See http://matplotlib.sourceforge.net/api/artist_api.html#module-matplotlib.patches for other drawable objects.

Sample output from first snippet:

Sample output from second snippet:

EDIT for Griff: If you want to handle a different radius for each point, you can do something straightforward like:

def my_circle_scatter_radii(axes, x_array, y_array, radii_array, **kwargs):
    for (x, y, r) in zip(x_array, y_array, radii_array):
        circle = pylab.Circle((x,y), radius=r, **kwargs)
        axes.add_patch(circle)
    return True

The logic to handle both cases simultaneously is also straightforward (e.g., check if its an radii_array is an int/float and if so make it an array).

这篇关于以相同的坐标绘制/分散位置和标记大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆