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画图实例之绘制拥有彩条的图表

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

相关文章

pygame库实现俄罗斯方块小游戏

pygame库实现俄罗斯方块小游戏

本文实例为大家分享了pygame库实现俄罗斯方块小游戏的具体代码,供大家参考,具体内容如下 import random,time,pygame,sys from pygame.loc...

python多进程下实现日志记录按时间分割

python多进程下实现日志记录按时间分割,供大家参考,具体内容如下 原理:自定义日志handler继承TimedRotatingFileHandler,并重写computeRollov...

python 定时器,实现每天凌晨3点执行的方法

如下所示: ''' Created on 2018-4-20 例子:每天凌晨3点执行func方法 ''' import datetime import threading def...

django框架模板语言使用方法详解

django框架模板语言使用方法详解

本文实例讲述了django框架模板语言使用方法。分享给大家供大家参考,具体如下: 模板功能 作用:生成html界面内容,模版致力于界面如何显示,而不是程序逻辑。模板不仅仅是一个html文...

Python3中正则模块re.compile、re.match及re.search函数用法详解

本文实例讲述了Python3中正则模块re.compile、re.match及re.search函数用法。分享给大家供大家参考,具体如下: re模块 re.compile、re.matc...