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函数中不定长参数的写法

Python函数中不定长参数的写法

1、不定长参数的写法,用 *变量名 表示 2、不定长参数累加 3、不定长参数,使用**c接受m=23,n=56的值; 传参时,a必写,b、c可以缺省 def fun(a, b,...

python 中的int()函数怎么用

int(x, [base]) 功能: 函数的作用是将一个数字或base类型的字符串转换成整数。 函数原型: int(x=0) int(x, base=10),base缺省值为10,也就是...

python 处理dataframe中的时间字段方法

在机器学习过程中,通常会通过pandas读取csv文件,保持成dadaframe格式,然而有时候需要对dataframe中的时间字段进行数据建模,比如时间格式为datetime,那么像一...

详解Python中字符串前“b”,“r”,“u”,“f”的作用

1、字符串前加 u 例:u"我是含有中文字符组成的字符串。" 作用: 后面字符串以 Unicode 格式 进行编码,一般用在中文字符串前面,防止因为源码储存格式问题,导致再次使用时出现乱...

详解python持久化文件读写

持久化文件读写: f=open('info.txt','a+') f.seek(0) str1=f.read() if len(str1)==0: f1 = open('info...