Python实现改变与矩形橡胶的线条的颜色代码示例

yipeiwu_com6年前Python基础

 与矩形相交的线条颜色为红色,其他为蓝色。

演示如下:

实例代码如下:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.transforms import Bbox
from matplotlib.path import Path

# Fixing random state for reproducibility
np.random.seed(19680801)


left, bottom, width, height = (-1, -1, 2, 2)
rect = plt.Rectangle((left, bottom), width, height, facecolor="#aaaaaa")

fig, ax = plt.subplots()
ax.add_patch(rect)

bbox = Bbox.from_bounds(left, bottom, width, height)

for i in range(12):
  vertices = (np.random.random((2, 2)) - 0.5) * 6.0
  path = Path(vertices)
  if path.intersects_bbox(bbox):
    color = 'r'
  else:
    color = 'b'
  ax.plot(vertices[:, 0], vertices[:, 1], color=color)

plt.show()

脚本运行时间:(0分0.026秒)

总结

以上就是本文关于Python实现改变与矩形橡胶的线条的颜色代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:

Python竟能画这么漂亮的花,帅呆了(代码分享)

Python学习之用pygal画世界地图实例

Python matplotlib画图实例之绘制拥有彩条的图表

如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

相关文章

python实现将一个数组逆序输出的方法

方法一: def printTheReverseArray(self): list_1 = [1, 2, 3, 4, 5, 6, 7] length = len(list_1...

详解pandas中MultiIndex和对象实际索引不一致问题

在最新版的pandas中(不知道之前的版本有没有这个问题),当我们对具有多层次索引的对象做切片或者通过df[bool_list]的方式索引的时候,得到的新的对象尽管实际索引已经发生了改变...

python使用Berkeley DB数据库实例

本文实例讲述了python使用Berkeley DB数据库的方法,分享给大家供大家参考。 具体实现方法如下: try: from bsddb import db except...

python 实现dict转json并保存文件

如下所示: import json f = open("index.html", "wb") json.dump(response.data, f) f.close() dum...

django请求返回不同的类型图片json,xml,html的实例

django 返回数据的主要是用django.http.HttpResponse 中的HttpResponse 完成的 具体的我直接贴代码吧 from django.http im...