【问题标题】:Creating random points on (x,y) graphic in Python在 Python 中的 (x,y) 图形上创建随机点
【发布时间】:2020-06-14 11:13:14
【问题描述】:

我需要在 (X,Y) 图形中创建随机点(最小 12 - 最大 18 个点)。

这个点必须在 0-9 x 和 y 之间。 Like this

import random
import matplotlib.pyplot as plt

x=[1,2,3,4,5,6,7,0,8,9]

y=[1,2,3,4,5,6,0,7,8,9]
print(x+y)

random.shuffle(x)

random.shuffle(y)

a=plt.scatter(x,y, color='k', s=100)

random.shuffle(x)

random.shuffle(y)

b=plt.scatter(x,y, color='k', s=100)

plt.show()

【问题讨论】:

  • 欢迎来到 stackoverflow :) 你已经尝试过了吗?你走了多远?你具体遇到了什么问题?
  • @PaulHankin 谢谢,我尝试了一些东西,我会编辑帖子并添加代码。但是当我在点上添加 0 值时,标签会更改为 0,2,4,6,8 但我需要 0,1,2,3,4 直到 9
  • @PaulHankin 老实说,我正在尝试一步一步地做到这一点: -- stackoverflow.com/questions/62333166/… .

标签: python algorithm matplotlib random


【解决方案1】:

这是你要找的吗:

import matplotlib.pyplot as plt
from random import randint

amt = randint(12,19)

x = [randint(1,9) for _ in range(amt)]
y = [randint(1,9) for _ in range(amt)]

plt.scatter(x,y)
plt.show()

【讨论】:

  • 我怎样才能在一个列表中获得像 [x,y] 这样的所有点坐标?
  • print(x) print(y)
  • 或者,for x,y in zip(x,y): print(x,y)
【解决方案2】:

试试这个:

import random

coords = []
amount_of_coords = 12+random.randint(0, 7)
print(amount_of_coords)

for x in range(amount_of_coords):
    coords.append([random.randint(0, 10),random.randint(0, 10)])

print(coords)

for point in coords:
    plt.scatter(point[0],point[1], color='k', s=100)

plt.show()

输出:

12
[[1, 1], [3, 7], [3, 1], [4, 0], [0, 7], [2, 1], [1, 0], [2, 8], [3, 4], [4, 6], [3, 7], [1, 4]]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-09
    • 2021-10-16
    • 2019-07-23
    • 1970-01-01
    • 2012-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多