Python 散点图。标记的大小和样式
我有一组数据想要显示为散点图。我希望将每个点绘制为大小为 dx 的正方形。
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]
scatter(x,y,c=z,s=dx,marker='s')
问题是分散函数读取的大小 s
单位为磅^2。我想要的是让每个点由面积 dx^2 的正方形表示,其中该面积采用“真实”单位,即绘图单位。我希望你能明白这一点。
我还有另一个问题。分散功能用黑色边框绘制标记,我怎样才能删除此选项并且完全没有边框?
I have a set of data that I want to show as a scatter plot. I want each point to be plotted as a square of size dx
.
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]
scatter(x,y,c=z,s=dx,marker='s')
The problem is that the size s
that the scatter function read is in points^2. What I'd like is having each point represented by a square of area dx^2, where this area is in 'real' units, the plot units. I hope you can get this point.
I also have another question. The scatter function plots the markers with a black border, how can I drop this option and have no border at all?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从用户数据坐标系转换为显示坐标系。
并使用 edgecolors='none' 绘制没有轮廓的面。
Translate from user data coordinate system to display coordinate system.
and use edgecolors='none' to plot faces with no outlines.
我认为我们可以通过一系列补丁来做得更好。
根据文件:
假设您想在数据单元中绘制具有给定半径的圆的散布图:
scatter
函数的所有参数和关键字(除了marker
)都将以类似的方式工作。我写了一个要点,其中包括圆圈、椭圆和正方形/矩形。如果你想要其他形状的集合,你可以自己修改。
如果您想绘制颜色条,只需运行
colorbar()
或将返回的集合对象传递给colorbar
函数。示例:
输出:
I think we can do it better with a collection of patches.
According to documents:
Suppose you want to plot a scatter of circles with given radius in data unit:
All the arguments and keywords (except
marker
) ofscatter
function would work in similar way.I've write a gist including circles, ellipses and squares/rectangles. If you want a collection of other shape, you could modify it yourself.
If you want to plot a colorbar just run
colorbar()
or pass the returned collection object tocolorbar
function.An example:
Output:
如果您希望标记随图形大小调整大小,可以使用补丁:
请注意:
(x,y)
为中心。 x,y 实际上是坐标左下角的正方形。我这样做是为了简化我的代码。你
应使用
(x + dx/2, y + dx/2)
。您还应该根据您的需要调整它
最后对于第二个问题。您可以使用关键字参数
edgecolor
或edgecolors
来获取散点标记的边框。这些分别是 matplotlib 颜色参数或 rgba 元组序列。如果将参数设置为“无”,则不会绘制边框。If you want markers that resize with the figure size, you can use patches:
Note that:
(x,y)
. x,y are actually the coords ofthe square lower left. I let it this way to simplify my code. You
should use
(x + dx/2, y + dx/2)
.you should also adapt this to your needs
Finally for your second question. You can get the border of the scatter marks out using the keyword arguments
edgecolor
oredgecolors
. These are a matplotlib color argument or a sequence of rgba tuples, respectively. If you set the parameter to 'None', borders are not draw.