我已经完成了一个图形的编码,如何使用相同的代码随机生成10个图形。我想要一个独特的图。
import matplotlib.pyplot as plt
# line 1 points
x1 = [1,2,3]
y1 = [2,4,1]
# plotting the line 1 points
plt.plot(x1, y1, label = "line 1")
# naming the x axis
plt.xlabel('x - axis')
# naming the y axis
plt.ylabel('y - axis')
# giving a title to my graph
plt.title('Two lines on same graph!')
# show a legend on the plot
plt.legend()`enter code here`
# function to show the plot
plt.show()
答案 0 :(得分:4)
如果我没记错的话,您只想要10张带有随机线条的图形。
从您的示例中,我假设您只需要整数值。
使用Numpy可以生成随机值列表。在下面的示例中,数组的长度为10,值的范围为0到15。
import numpy as np
x = np.random.randint(low=0, high=15, size=10)
y = np.random.randint(low=0, high=15, size=10)
我不明白您是否还要求此数组必须与众不同。
让我们继续绘制10个随机图:
import matplotlib.pyplot as plt
def draw_plot(sub):
"""Draw a random graph."""
# line 1 points
x = np.sort(np.random.randint(low=0, high=15, size=10))
y = np.random.randint(low=0, high=15, size=10)
# plotting the line 1 points
sub.plot(x, y, label = "line 1")
# naming the x axis
sub.set_xlabel('x - axis')
# naming the y axis
sub.set_ylabel('y - axis')
# giving a title to my graph
sub.set_title('Two lines on same graph!')
# show a legend on the plot
sub.legend()
plt.figure(figsize=(20, 8))
for i in range(10):
sub = plt.subplot(2, 5, i+1)
draw_plot(sub)
plt.show()