编程能做哪些中看不中用的东西

浏览: 2383

废话少说,先上图!


1. Python


    这三颗树,就是使用python3,借助cairocffi包绘制的树,实在太美,就在这里分享给大家啦。具体代码如下。需要注意的是:cairocffi包难在windows下直接pip install安装,可以选择Ubuntu下安装使用。

# -*- coding: utf-8 -*-1"""2~~~~~~~~~~~~~~~~~~~~~~~~~~~~3A Simple Random Fractal Tree4~~~~~~~~~~~~~~~~~~~~~~~~~~~~5"""6import random7import math8import cairocffi as cairo9
10
11IMAGE_WIDTH = 60012IMAGE_HEIGHT = 60013# ------ params control the appearance of the tree ----------14ITERATIONS = 16  # total number of iterations
ROOT_COLOR = (0, 0, 0) # root branch color
LEAF_COLOR = (1.0, 1.0, 0.2) # leaf color
TRUNK_LEN = 200  # initial length of the trunk
TRUNK_RAD = 3  # initial radius of the trunk
SCALE = 0.8  # contraction factor between successive trunks
THETA = math.pi / 2  # initial angle of the branch
ANGLE = math.pi / 4.5  # angle between branches in the same level
ROOT = (IMAGE_WIDTH/2.0, IMAGE_HEIGHT+50) # pixel position of the root
PERTURB = 6.0  # perturb the angle a little to make the tree look random
# ----------------------------------------------------------
def get_color(level, root_color, tip_color):
"""Return an interpolation of the two colors `root_color` and `tip_color`.
  """
return ((level*1.0/ITERATIONS)*(root_color[0]-tip_color[0])+tip_color[0],
(level*1.0/ITERATIONS)*(root_color[1]-tip_color[1])+tip_color[1],
(level*1.0/ITERATIONS)*(root_color[2]-tip_color[2])+tip_color[2])
def get_line_width(level):
"""Return the line width of a given level."""
return max(1, TRUNK_RAD*level/ITERATIONS)
def fractal_tree(ctx,     # a cairo context to draw on
iterations, # number of iterations
start,    # x,y coordinates of the start of this branch
t,      # current trunk length
r,      # factor to contract the trunk each iteration
theta,    # starting orientation
angle,    # angle between branches in the same iteration
perturb,   # perturb the angle
root_color, # root color
tip_color): # leaf color
if iterations == 0:
return
x0, y0 = start
# randomize the length
randt = random.random()*t
x, y = x0 + randt * math.cos(theta), y0 - randt * math.sin(theta)
# color the branch according to its position in the tree
color = get_color(iterations, root_color, tip_color)
# draw the branch
ctx.move_to(x0, y0)
ctx.line_to(x, y)
ctx.set_line_width(get_line_width(iterations))
ctx.set_source_rgb(*color)
ctx.stroke()
# recursive draw next branches
fractal_tree(ctx, iterations-1, (x, y), t*r, r,
theta + (random.random())*(perturb/(iterations+1))*angle,
angle, perturb, root_color, tip_color)
fractal_tree(ctx, iterations-1, (x, y), t*r, r,
theta - (random.random())*(perturb/(iterations+1))*angle,
angle, perturb, root_color, tip_color)
def main():
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, IMAGE_WIDTH, IMAGE_HEIGHT)
ctx = cairo.Context(surface)
ctx.set_line_cap(cairo.LINE_CAP_ROUND)
ctx.set_line_join(cairo.LINE_JOIN_ROUND)
ctx.set_source_rgb(1, 1, 1)
ctx.paint()
fractal_tree(ctx, ITERATIONS, ROOT, TRUNK_LEN, SCALE,
THETA, ANGLE, PERTURB, ROOT_COLOR, LEAF_COLOR)
surface.write_to_png("random_fractal_tree.png")
85
86if __name__ == "__main__":87main()


2. Matlab


image.png

之前无意中逛论坛看到有人用matlab绘制3D-Heart,觉得很好玩,效果图如下图所示:

image.png

主要是使用如下的公式:

image.png

其中-3≤x, y, z≤3。然后我自己也用maltab使用line()函数实现绘制,同时可以改变参数(x, y, z的步长)实现,线条颜色RGB为(255,104,181),得到如下不同的效果图。Line()函数是很基础的函数,所以根据基本公式,使用R语言可以使用plot3()绘制成功。


3. R


image.png


这些蝴蝶就是使用R 语言的mathart包生成的数据,使用ggplot2散点图绘制而成的,具体代码如下:

Install the packages

Load the libraries

Create butterflies

需要注意的是:R的版本是3.3.2,程序包'mathart'要求版本为>= 3.4.3的R

如果你是在windows环境下,可以安装一个包:installr。不仅能帮你更新R,还能将原来安装的扩展包配置到新版本下,不用再重新安装。

library(installr)
updateR()

史上最全的图表色彩运用原理

【重磅】史上最全的论文图表基本规范

图表绘制的必备技能

学术图表的基本配色方法

商业图表的基本配色方法

如需转载,请留言联系EasyCharts团队


【书籍推荐】《Excel 数据之美--科学图表与商业图表的绘制》

【手册获取】国内首款-数据可视化参考手册:专业绘图必备

【必备插件】  EasyCharts -- Excel图表插件

【网易云课堂】  Excel 商业图表修炼秘笈之基础篇



推荐 0
本文由 EasyCharts 创作,采用 知识共享署名-相同方式共享 3.0 中国大陆许可协议 进行许可。
转载、引用前需联系作者,并署名作者且注明文章出处。
本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责。本站是一个个人学习交流的平台,并不用于任何商业目的,如果有任何问题,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

0 个评论

要回复文章请先登录注册