matplotlib-如何绘制一个面向随机的矩形(或任何形状)? [英] matplotlib - How to plot a random-oriented rectangle (or any shape)?

查看:57
本文介绍了matplotlib-如何绘制一个面向随机的矩形(或任何形状)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想画一条线,其宽度在数据单元中指定.在这种情况下,只需执行

  plot(x,y,linewidth = 1)

会失败,因为 linewidth 没有在数据单元中指定.

为此,我找到

所以,掌握了Path,基本上可以随便画什么画.现在让我们看看如何使用神奇的 Path 实现您的目标.

要获得您在问题中提到的矩形,只需对示例进行一点改动即可.

 将matplotlib.pyplot导入为plt从 matplotlib.path 导入路径导入matplotlib.patches作为补丁verts = [(0.,0.),#左,底(-1., 1.), # 左,上(1., 3.), # right, top(2., 2.), # 右边,底部(0.,0.),]#已忽略代码 = [Path.MOVETO,路径LINETO,路径.LINETO,路径.LINETO,路径.CLOSEPOLY,]路径=路径(版本,代码)无花果= plt.figure()ax = fig.add_subplot(111)patch = patch.PathPatch(path, facecolor='orange', lw=2)ax.add_patch(补丁)ax.axis('等于')plt.show()

我认为代码是如此直接和不言自明,以至于我不需要在上面浪费我的文字.只需复制并粘贴并运行它,您就会得到这个,正是您想要的.

I wish to draw a line whose width is specified in the data unit. In this case, simply doing

plot(x, y, linewidth=1)

would fail, as the linewidth there is NOT specified in data unit.

To do this, I find fill_between(), but I find all the examples given here are of the format

fill_between(x, y1, y2)

which means, the x is always shared by y1 and y2.

So what if y1 and y2 don't share the same x?

e.g. I wish to fill between line1=[(0, 0), (2, 2)] and line2=[(-1, 1), (1, 3)] (essentially, they form a rectangle). In this case, I want something like

fill_between(x1, x2, y1, y2)

Apparently, it is not working as desired:

In [132]: x1 = [0,2]
   .....: x2 = [-1, 1]
   .....: y1 = [0,2]
   .....: y2 = [1,3]
   .....: fill_between(x1, x2, y1, y2)
   .....: 
Out[132]: <matplotlib.collections.PolyCollection at 0x3e5b230>

How should I plot in this case?

解决方案

Good question! I would suggest you that do not limit yourself in the fill_between function. I always regard it as beneficial to look deep inside the things. Let's dive into the essence of the Python drawing.

The object underlying all of the matplotlib.patch objects is the Path.

Therefore, if you master the Path, you basically can draw whatever you like in whatever manner. Let's now see how do we achieve your goal with the magical Path.

To get the rectangle you mentioned in the question, only a bit of adaption on the example is needed.

import matplotlib.pyplot as plt
from matplotlib.path import Path
import matplotlib.patches as patches

verts = [(0., 0.), # left, bottom
         (-1., 1.), # left, top
         (1., 3.), # right, top
         (2., 2.), # right, bottom
         (0., 0.),] # ignored

codes = [Path.MOVETO,
         Path.LINETO,
         Path.LINETO,
         Path.LINETO,
         Path.CLOSEPOLY,]

path = Path(verts, codes)
fig = plt.figure()
ax = fig.add_subplot(111)
patch = patches.PathPatch(path, facecolor='orange', lw=2)
ax.add_patch(patch) 
ax.axis('equal')
plt.show()

I think the code is so straightforward and self-explanatory that I do not need to waste my words on it. Just copy and paste and run it, you will get this, exactly what you want.

这篇关于matplotlib-如何绘制一个面向随机的矩形(或任何形状)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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