【发布时间】:2019-07-09 02:04:21
【问题描述】:
我想用 numpy 创建一个新图像,我想创建的图像是这样的:
不显示半圆并发送消息说 “使用 RGB 数据将输入数据剪切到 imshow 的有效范围(浮点数为 [0..1] 或整数为 [0..255]) 。”
我的代码是这样的:
import math
import numpy as np
import matplotlib.pyplot as plt
new_image = np.zeros((300, 250, 3))
for x in range(300):
for y in range(250):
if (180 <= x <= 240) and (100 <= y <=200):
new_image[x, y , 2] = 255
elif (90 <= x <= 150) and ((-2*x/3)+110 <= y <= (2*x/3)-10):
new_image[x,y,0] = 255
elif (60 <= x <=120) and (150 <= y <= 200+math.sqrt(900-(x-90)^2)):
new_image[x,y,0] = 255
new_image[x,y,1] = 255
else:
new_image[x,y,0] = 255
new_image[x,y,1] = 255
new_image[x,y,2] = 255
# First Plot
plt.figure()
plt.imshow(new_image)
plt.axis('off')
plt.show()
【问题讨论】:
-
plt.imshow((new_image).astype(np.uint8))应该删除警告。你能解释一下elif (60 <= x <=120) and (150 <= y <= 200+math.sqrt(900-(x-90)^2)):背后的原因吗? -
请注意,python 中的
^表示XOR- 如果您想将某个x 提高到2 的幂,请使用x ** 2。 -
@Jeppe 这是因为这是图形的方程,我们在 Matlab 中也这样做,但我们需要将代码传递给 python,但在 Python 中不显示半圆。
-
@Jeppe 谢谢我忘了改变它。现在有效。
-
问题与
scikit-learn或scipy无关 - 请不要向无关标签发送垃圾邮件(已删除并替换为matplotlib)。
标签: python-3.x numpy matplotlib