使用PyCharm创造现代艺术(一)

前言

在我们的生活中有许多天生的艺术家,他(她)们拿起笔来就可以创造艺术,在屏幕前的你是不是这样呢?反正我不是。不过我可以通过编程做一些抽象艺术(非抽象画都画不了,何况做出来呢?)。今天我们就来使用PyCharm制作现代艺术——散点图。

方法一:使用matplotlib制作散点图(推荐)

安装matplotlib

首先,打开PyCharm,单击上方的File按钮,在弹出的菜单中选择Settings。

使用PyCharm创造现代艺术(一)

弹出以下界面就对了。

使用PyCharm创造现代艺术(一)

选中以”Project:” 开头的按钮,在弹出的界面中选择”Python Interpreter”。

使用PyCharm创造现代艺术(一)

单击上方的加号。(PyCharm2021以前的版本加号在右面)

使用PyCharm创造现代艺术(一)

使用PyCharm创造现代艺术(一)

在上方的搜索栏中搜索”matplotlib”。

使用PyCharm创造现代艺术(一)

单击”Install Package”,安装matplotlib。

使用PyCharm创造现代艺术(一)

出现绿色长条就安装成功了。如果出现红条,请检查一下网络再试。

使用PyCharm创造现代艺术(一)

至此,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()

运行结果如下:

使用PyCharm创造现代艺术(一)

源代码

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/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • Python pandas.Series.str.cat实例讲解

    Series.str.cat(others=None, sep=None, na_rep=None, join=’left’) 使用给定的分隔符连接系列/索引中的字符串。 如果指定…

    Python 2023年8月7日
    074
  • 别再用 requirements.txt 来管理依赖了

    在我第一次用到 requirements.txt 时,是在一个虚拟环境中,我使用 pip freeze > requirements.txt 就把项目中的依赖项导出到了 tx…

    Python 2023年8月5日
    051
  • Windows 10 – Python 的虚拟环境 Virtualenv – 全局 python 环境切换问题

    目录 1. 虚拟环境 2. 解决全局 python 环境切换的 bug * virtualenv 虚拟环境与本地 python 环境的测试 3. virtualenv 虚拟环境 个…

    Python 2023年8月4日
    069
  • 【无标题】

    参与任务 中文拼写检查任务是中文自然语言处理中非常具有代表性和挑战性的任务,其本质是找出文本段落中的错别字。这项任务在各种领域,如公文,新闻、财报中都有很好的落地应用价值。而其任务…

    Python 2023年9月27日
    037
  • 【Linux】调试器gdb的使用

    目录 * – 👉什么是 gdb👈 – 👉gdb 的使用👈 – + 查看代码 + 设置断点 + 查看断点 + 删除断点 + 执行当前被调试的程序 …

    Python 2023年10月8日
    039
  • 机器学习-感知机模型

    一、引言 单层感知机是神经网络的一个基本单元,类似于人类的神经网络的一个神经元,神经网络是由具有适应性的简单单元(感知机)组成的广泛并行互连的网络,它的组织能够模拟生物神经系统对真…

    Python 2023年10月25日
    041
  • Python函数/动态参数/关键字参数

    1.函数 #函数语法: #函数名规范:小谢字母开头,不同字母下划线隔开(字母数字下划线) #def 函数名(): #函数体:希望函数做的事情 1.1.无参函数 #无参函数 def …

    Python 2023年10月30日
    033
  • 11【SpringMVC的文件上传】

    文章目录 五、文件上传 * 5.1 回顾文件上传 5.2 SpringMVC处理文件上传 – 5.2.1 配置文件上传解析器 5.3.2 准备表单 5.3.3 编写处理…

    Python 2023年9月29日
    031
  • pip/conda导出 requirements.txt 注意事项

    Python 提供了强大的模块功能,能够方便开发者更加易于进行包的管理。怎么将当前环境的安装包依赖信息导出呢?pip/conda提供了生成 requirements.txt 的功能…

    Python 2023年9月7日
    068
  • 【Python 实战基础】如何绘制饼状图分析商品库存

    一、实战场景 二、主要知识点 文件读写 基础语法 字符串处理 文件生成 数据构建 三、菜鸟实战 1、创建 python 文件 2、运行结果 实战场景:如何绘制饼状图分析商品库存 文…

    Python 2023年8月15日
    057
  • SQL–基础语句

    1 SELECT skucolor,clicks FROM shecharme;#SQL语句从Shecharme表&#x4E…

    Python 2023年6月10日
    064
  • FastAPI 学习之路(十四)响应模型

    系列文章: FastAPI 学习之路(一)fastapi–高性能web开发框架 FastAPI 学习之路(二) FastAPI 学习之路(三) FastAPI 学习之路…

    Python 2023年5月25日
    065
  • cv2库、numpy库、matplotlib.pyplot库-树莓派 Opencv-基于Python学习记录DAY-1

    首先是接触到了3个常用的python库 :numpy,cv2,matplotlib.pyplot。 在开始前博主因为没装matplotlib.pyplot而报了次错,这里贴一下,安…

    Python 2023年9月3日
    035
  • 【Pygame小游戏】Python版有迷宫嘛?原来藏在个地方呀~

    💦前言 大家好,是我: 一大早高清无码好东西刚到手 就给大家安排推文的栗子同学上线·放牛小编——一切随缘更新+慢更新的我! 决心痛改前非,每周争取能多写几篇。(这个flag立在这里…

    Python 2023年9月22日
    033
  • Python ❀ 软件介绍

    Python学习计划(一) 一、定义软件:按特定顺序组织的计算机数据和指令的集合 [En] Software: a collection of computer data and …

    Python 2023年5月25日
    072
  • 不懂 Kubernetes 实现云原生是什么体验?

    云原生的本质和最终效果 要明白什么是云原生,就要先弄明白云计算是什么有什么问题,云计算将计算资源、网络、存储等基础设施统一管理,通过资源规模化和自动化管理,实现降低资源的成本和提高…

    Python 2023年10月22日
    043
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球