前言
在我们的生活中有许多天生的艺术家,他(她)们拿起笔来就可以创造艺术,在屏幕前的你是不是这样呢?反正我不是。不过我可以通过编程做一些抽象艺术(非抽象画都画不了,何况做出来呢?)。今天我们就来使用PyCharm制作现代艺术——散点图。
方法一:使用matplotlib制作散点图(推荐)
安装matplotlib
首先,打开PyCharm,单击上方的File按钮,在弹出的菜单中选择Settings。
弹出以下界面就对了。
选中以”Project:” 开头的按钮,在弹出的界面中选择”Python Interpreter”。
单击上方的加号。(PyCharm2021以前的版本加号在右面)
在上方的搜索栏中搜索”matplotlib”。
单击”Install Package”,安装matplotlib。
出现绿色长条就安装成功了。如果出现红条,请检查一下网络再试。
至此,matplotlib就安装成功了。
编写程序
RandomWalk类
为了简化制作散点图的程序,我们先编写一个模拟随机漫步的类,它有2个方法:
方法名和参数作用__init__(self, num_points=50000)初始化点数、并调用fill_walk方法,记录每个点的位置fill_walk(self)产生每个点的位置,拒绝原地踏步和走到之前走过的位置
下面是方法__init__的代码。
def __init__(self, num_points=50000):
self.num_points = num_points
self.x_values = [0]
self.y_values = [0]
self.fill_walk()
由于知道了点数,在fill_walk方法中,我们可以使用for循环,但是由于我们要拒绝原地踏步和走到之前走过的位置,所以我们是用while循环和continue语句完成。
fill_walk的代码如下:
from random import choice as c
def fill_walk(self):
while len(self.x_values) < self.num_points:
x_direction = c([3, 2, 1, -1, -2, -3])
x_distance = c([0, 1, 2, 3, 4, 5, 6, 7, 8])
x_step = x_direction * x_distance
y_direction = c([3, 2, 1, -1, -2, -3])
y_distance = c([0, 1, 2, 3, 4, 5, 6, 7, 8])
y_step = y_direction * y_distance
if x_step == 0 and y_step == 0:
continue
next_x = self.x_values[-1] + x_step
next_y = self.y_values[-1] + y_step
if next_x in self.x_values and next_y in self.y_values:
continue
self.x_values.append(next_x)
self.y_values.append(next_y)
整个RandomWalk类的代码如下,把它保存成random_walk.py。
random_walk.py
from random import choice as c
class RandomWalk:
def __init__(self, num_points=50000):
self.num_points = num_points
self.x_values = [0]
self.y_values = [0]
self.fill_walk()
def fill_walk(self):
while len(self.x_values) < self.num_points:
x_direction = c([3, 2, 1, -1, -2, -3])
x_distance = c([0, 1, 2, 3, 4, 5, 6, 7, 8])
x_step = x_direction * x_distance
y_direction = c([3, 2, 1, -1, -2, -3])
y_distance = c([0, 1, 2, 3, 4, 5, 6, 7, 8])
y_step = y_direction * y_distance
if x_step == 0 and y_step == 0:
continue
next_x = self.x_values[-1] + x_step
next_y = self.y_values[-1] + y_step
if next_x in self.x_values and next_y in self.y_values:
continue
self.x_values.append(next_x)
self.y_values.append(next_y)
编写随机漫步的程序
编写好了random_walk.py,我们就可以编写随机漫步的程序了,它叫rw_for_once.py,
rw_for_once.py
import matplotlib.pyplot as plt
from random_walk import RandomWalk
rw = RandomWalk()
plt.figure(dpi=128, figsize=(10, 7))
point_numbers = list(range(rw.num_points))
plt.scatter(rw.x_values, rw.y_values, s=0.1, c=point_numbers, cmap=plt.cm.Greens,
edgecolors=None)
plt.show()
运行结果如下:
源代码
random_walk.py
from random import choice as c
class RandomWalk:
def __init__(self, num_points=50000):
self.num_points = num_points
self.x_values = [0]
self.y_values = [0]
self.fill_walk()
def fill_walk(self):
while len(self.x_values) < self.num_points:
x_direction = c([3, 2, 1, -1, -2, -3])
x_distance = c([0, 1, 2, 3, 4, 5, 6, 7, 8])
x_step = x_direction * x_distance
y_direction = c([3, 2, 1, -1, -2, -3])
y_distance = c([0, 1, 2, 3, 4, 5, 6, 7, 8])
y_step = y_direction * y_distance
if x_step == 0 and y_step == 0:
continue
next_x = self.x_values[-1] + x_step
next_y = self.y_values[-1] + y_step
if next_x in self.x_values and next_y in self.y_values:
continue
self.x_values.append(next_x)
self.y_values.append(next_y)
rw_for_once.py
import matplotlib.pyplot as plt
from random_walk import RandomWalk
rw = RandomWalk()
plt.figure(dpi=128, figsize=(10, 7))
point_numbers = list(range(rw.num_points))
plt.scatter(rw.x_values, rw.y_values, s=0.1, c=point_numbers, cmap=plt.cm.Greens,
edgecolors=None)
plt.show()
方法二:使用Turtle库制作散点图
编写程序
RandomWalk类
同样,我们使用Turtle库,也需要一个RandomWalk类。不过,它的方法可不太一样,让我们看看吧!
方法名和参数作用__init__(self,num_points=50000,screen_width,screen_height)初始化点数、并确定GUI大小(确定点数不超过GUI面积,否则引发ValueError异常)、并调用fill_walk方法,记录每个点的位置fill_walk(self)产生每个点的位置,拒绝原地踏步和走到之前走过的位置以及拒绝超出边界
下面是__init__方法的代码。
def __init__(self, screen_width, screen_height, num_points=50000):
self.num_points = num_points
self.screen = [screen_width, screen_height]
if self.screen[0] * self.screen[1]
fill_walk的方法如下:
from random import choice as c
def fill_walk(self):
while len(self.x_values) < self.num_points:
x_direction = c([3, 2, 1, -1, -2, -3])
x_distance = c([0, 1, 2, 3, 4, 5, 6, 7, 8])
x_step = x_direction * x_distance
y_direction = c([3, 2, 1, -1, -2, -3])
y_distance = c([0, 1, 2, 3, 4, 5, 6, 7, 8])
y_step = y_direction * y_distance
if x_step == 0 and y_step == 0:
continue
next_x = self.x_values[-1] + x_step
next_y = self.y_values[-1] + y_step
if next_x in self.x_values and next_y in self.y_values:
continue
if next_x > self.screen[0] / 2 or next_x < self.screen[0] * -1 / 2:
continue
if next_y > self.screen[1] / 2 or next_y < self.screen[1] * -1 / 2:
continue
self.x_values.append(next_x)
self.y_values.append(next_y)
整个RandomWalk类就编写好了,我们把它保存为random_walk.py。
random_walk.py
from random import choice as c
class RandomWalk:
def __init__(self, screen_width, screen_height, num_points=50000):
self.num_points = num_points
self.screen = [screen_width, screen_height]
if self.screen[0] * self.screen[1] self.screen[0] / 2 or next_x < self.screen[0] * -1 / 2:
continue
if next_y > self.screen[1] / 2 or next_y < self.screen[1] * -1 / 2:
continue
self.x_values.append(next_x)
self.y_values.append(next_y)
编写产生随机漫步的程序
使用Turtle库编程,我们需要定义一个函数——draw_dot。
import turtle
def draw_dot(x, y):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.dot(1)
这个程序如下(由于Turtle画图太慢,这里就没有展示了):
rw_for_once.py
import turtle
from random_walk import RandomWalk
def draw_dot(x:int, y:int):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.dot(3)
turtle.title("Random Walk")
turtle.screensize(600, 480)
turtle.speed(0)
turtle.color("blue")
turtle.hideturtle()
rw = RandomWalk(600, 480)
for j in range(len(rw.x_values)):
draw_dot(rw.x_values[j], rw.y_values[j])
turtle.done()
源代码
random_walk.py
from random import choice as c
class RandomWalk:
def __init__(self, screen_width, screen_height, num_points=50000):
self.num_points = num_points
self.screen = [screen_width, screen_height]
if self.screen[0] * self.screen[1] self.screen[0] / 2 or next_x < self.screen[0] * -1 / 2:
continue
if next_y > self.screen[1] / 2 or next_y < self.screen[1] * -1 / 2:
continue
self.x_values.append(next_x)
self.y_values.append(next_y)
rw_for_once.py
import turtle
from random_walk import RandomWalk
def draw_dot(x:int, y:int):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.dot(3)
turtle.title("Random Walk")
turtle.screensize(600, 480)
turtle.speed(0)
turtle.color("blue")
turtle.hideturtle()
rw = RandomWalk(600, 480)
for j in range(len(rw.x_values)):
draw_dot(rw.x_values[j], rw.y_values[j])
turtle.done()
后记
今天我们创作了一幅抽象画,下一次我们还要创建一幅《矩形的世界》。
Original: https://blog.csdn.net/wugang0131/article/details/123082227
Author: wugang0131
Title: 使用PyCharm创造现代艺术(一)
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/768610/
转载文章受原作者版权保护。转载请注明原作者出处!