用python画一朵玫瑰(用Python画玫瑰)

http://www.itjxue.com  2023-02-04 10:11  来源:未知  点击次数: 

python怎么画玫瑰花

操纵海龟绘图有着许多的命令,这些命令可以划分为两种:一种为运动命令,一种为画笔控制命令

1. 运动命令:

forward(degree) ?#向前移动距离degree代表距离

backward(degree) #向后移动距离degree代表距离

right(degree) ? ?#向右移动多少度

left(degree) ? ? ?#向左移动多少度

goto(x,y) ? ? ? ? ? #将画笔移动到坐标为x,y的位置

stamp() ? ? ? ? ? #复制当前图形

speed(speed) ? ? #画笔绘制的速度范围[0,10]整数

2. 画笔控制命令:

down() #移动时绘制图形,缺省时也为绘制

up() ? ? ?#移动时不绘制图形

pensize(width) ? ? #绘制图形时的宽度

color(colorstring) #绘制图形时的颜色

fillcolor(colorstring) #绘制图形的填充颜色

fill(Ture)

fill(false)

lucy : 梦想照进现实;露茜;青春风采;

draw_flower1.py

[python]?view plain?copy

#?-*-?coding:?cp936?-*-

import?turtle

import?math

def?p_line(t,?n,?length,?angle):

"""Draws?n?line?segments."""

for?i?in?range(n):

t.fd(length)

t.lt(angle)

def?polygon(t,?n,?length):

"""Draws?a?polygon?with?n?sides."""

angle?=?360/n

p_line(t,?n,?length,?angle)

def?arc(t,?r,?angle):

"""Draws?an?arc?with?the?given?radius?and?angle."""

arc_length?=?2?*?math.pi?*?r?*?abs(angle)?/?360

n?=?int(arc_length?/?4)?+?1

step_length?=?arc_length?/?n

step_angle?=?float(angle)?/?n

#?Before?starting?reduces,?making?a?slight?left?turn.

t.lt(step_angle/2)

p_line(t,?n,?step_length,?step_angle)

t.rt(step_angle/2)

def?petal(t,?r,?angle):

"""Draws?a?花瓣?using?two?arcs."""

for?i?in?range(2):

arc(t,?r,?angle)

t.lt(180-angle)

def?flower(t,?n,?r,?angle,?p):

"""Draws?a?flower?with?n?petals."""

for?i?in?range(n):

petal(t,?r,?angle)

t.lt(p/n)

def?leaf(t,?r,?angle,?p):

"""Draws?a?叶子?and?fill?it."""

t.begin_fill()?#?Begin?the?fill?process.

t.down()

flower(t,?1,?40,?80,?180)

t.end_fill()

def?main():

window=turtle.Screen()?#creat?a?screen

window.bgcolor("blue")

lucy=turtle.Turtle()

lucy.shape("turtle")

lucy.color("red")

lucy.width(5)

lucy.speed(0)

#?Drawing?flower

flower(lucy,?7,?60,?100,?360)

#?Drawing?pedicel

lucy.color("brown")

lucy.rt(90)

lucy.fd(200)

#?Drawing?leaf

lucy.rt(270)

lucy.color("green")

leaf(lucy,?40,?80,?180)

lucy.ht()

window.exitonclick()

main()

python气象绘图windrose

#导入包

import numpy as np

import pandas as pd

from matplotlib import pyplot as plt

from matplotlib.ticker import FuncFormatter

import matplotlib as mpl

mpl.rcParams['font.sans-serif'] = ['SimHei']? #设置简黑字体

mpl.rcParams['axes.unicode_minus'] = False? #设置负号正常显示

#----获取数据DataFrames,index*columns。index表示不同值范围,columns表示十六个风向

data = pd.DataFrame(wind_d_max_num_per,

? ? ? ? ? ? ? ? ? ? index=['15', '15~25', '25~35', '35~45',"≥45"],

? ? ? ? ? ? ? ? ? ? columns='N NNE NE ENE E ESE SE SSE S SSW SW WSW W WNW NW NNW'.split())

N = 16 # 风速分布为16个方向

theta = np.linspace(0, 2*np.pi, N, endpoint=False) # 获取16个方向的角度值

width = np.pi / 4 * 0.4? # 绘制扇型的宽度,可以自行调整0.5时是360,充满,有间隔的话小于0.5即可

labels = list(data.columns) # 自定义坐标标签为 N , NSN, ……# 开始绘图

plt.figure(figsize=(6,6),dpi=600)

ax = plt.subplot(111, projection='polar')

#----自定义颜色

mycolor =['cornflowerblue','orange','mediumseagreen','lightcoral','cyan']

#----循环画风玫瑰图

i=0

for idx in data.index:

? ? print(idx)

? ? # 每一行绘制一个扇形

? ? radii = data.loc[idx] # 每一行数据

? ? if i == 0:

? ? ? ? ax.bar(theta, radii, width=width, bottom=0.0, label=idx, tick_label=labels,

? ? ? ? ? color=mycolor[i])

? ? else:

? ? ? ? ax.bar(theta, radii, width=width, bottom=np.sum(data.loc[data.index[0:i]]), label=idx, tick_label=labels,

? ? ? ? ? color=mycolor[i])

? ? i=i+1

#此种画法,注意bottom设置,第一个bottom为0,后续bottom需要在前一个基础上增加。

ax.set_xticks(theta)

ax.set_xticklabels(labels,fontdict={'weight':'bold','size':15,'color':'k'})

ax.set_theta_zero_location('N') #设置零度方向北

ax.set_theta_direction(-1)? ? # 逆时针方向绘图

#----设置y坐标轴以百分数显示

plt.gca().yaxis.set_major_formatter(FuncFormatter(lambda s, position: '{:.0f}%'.format(100*s)))

plt.legend(loc=4, bbox_to_anchor=(0.05, -0.25),fontsize=12) # 将label显示出来, 并调整位置

#----保存图片

plt.savefig("./windrose1.svg")

怎么用python画玫瑰花,求大神贴代码,感激不尽

import?turtle

#?设置初始位置

turtle.penup()

turtle.left(90)

turtle.fd(200)

turtle.pendown()

turtle.right(90)

#?花蕊

turtle.fillcolor("red")

turtle.begin_fill()

turtle.circle(10,?180)

turtle.circle(25,?110)

turtle.left(50)

turtle.circle(60,?45)

turtle.circle(20,?170)

turtle.right(24)

turtle.fd(30)

turtle.left(10)

turtle.circle(30,?110)

turtle.fd(20)

turtle.left(40)

turtle.circle(90,?70)

turtle.circle(30,?150)

turtle.right(30)

turtle.fd(15)

turtle.circle(80,?90)

turtle.left(15)

turtle.fd(45)

turtle.right(165)

turtle.fd(20)

turtle.left(155)

turtle.circle(150,?80)

turtle.left(50)

turtle.circle(150,?90)

turtle.end_fill()

#?花瓣1

turtle.left(150)

turtle.circle(-90,?70)

turtle.left(20)

turtle.circle(75,?105)

turtle.setheading(60)

turtle.circle(80,?98)

turtle.circle(-90,?40)

#?花瓣2

turtle.left(180)

turtle.circle(90,?40)

turtle.circle(-80,?98)

turtle.setheading(-83)

#?叶子1

turtle.fd(30)

turtle.left(90)

turtle.fd(25)

turtle.left(45)

turtle.fillcolor("green")

turtle.begin_fill()

turtle.circle(-80,?90)

turtle.right(90)

turtle.circle(-80,?90)

turtle.end_fill()

turtle.right(135)

turtle.fd(60)

turtle.left(180)

turtle.fd(85)

turtle.left(90)

turtle.fd(80)

#?叶子2

turtle.right(90)

turtle.right(45)

turtle.fillcolor("green")

turtle.begin_fill()

turtle.circle(80,?90)

turtle.left(90)

turtle.circle(80,?90)

turtle.end_fill()

turtle.left(135)

turtle.fd(60)

turtle.left(180)

turtle.fd(60)

turtle.right(90)

turtle.circle(200,?60)

运行结果:

python音乐可视化:好玩的matplotlib南丁格尔玫瑰图版

效果图:

操作演示:

技术要点:

1 matplotlib的南丁格尔玫瑰图,用极坐标polar制作,并动画显示。

2 pygame新版的播放mp3,但本机的操作系统不能播放mp3,我用pydub做些格式转换。

3 用librosa获取音乐的相关数据和采样。

4 参考代码,并对源代码进行修改,增加,删减,排版和注释,感谢原作者,如有侵权,请联系,定删除。

====下面分步,讲解代码====

第1步:模块导入

第2步:窗口的初始化设置

第3步:参数设置

第4步:核心代码:

第5步:filter类

第6步:函数定义

第7步:启动主函数

自己整理,分享出来,希望大家喜欢。

python-繁花曲线规图案

python-繁花曲线规图案

[简介]

繁花曲线规现已成为儿童的一种智力 玩具 和设计工具,它由一套 彩色 塑料齿轮组成。一个内齿轮是环状的,齿做在里面;几个小外齿轮的齿做在外面,外齿轮内部有一些小圆孔和几个其它形状的、较大的孔。

[原理]

大小齿轮的齿数之比,约为 最简分数 时,其分母就是小齿轮的自转数,分母与分子之和就是 图案 中的花瓣数。而 分子 就是小齿轮沿着大齿轮的公转数。所以,只要掌握这个最简分数,就能知道画出来的图案大概是什么形状的。

总而言之,选择不同的齿轮与不同的孔,就可画出细腻、动人的各种曲线,例如玫瑰线、内摆线等等。

用繁花曲线规完成的图案:

下面我们一起用python来画一个繁花曲线规完成的漂亮图案:

首先我们先导入turtle:

第二步:定义画笔:

第三步:根据for循环和角度的确认完成图案的绘制

最后我们一起来看一下效果:

[总结]我们今天是用turtle来绘制的繁花曲线规的图案,并且应用for循环和角度的确认来完成。之前我们看用尺来画的都是彩色的,那小朋友能不能把我们今天绘制的图案变成彩色的呢?

用Python matplotlib 怎么画风向玫瑰图 能给出程序的

import?numpy?as?np

import?matplotlib.pyplot?as?plt

N?=?20

theta?=?np.linspace(0.0,?2?*?np.pi,?N,?endpoint=False)

radii?=?10?*?np.random.rand(N)

width?=?np.pi?/?4?*?np.random.rand(N)

ax?=?plt.subplot(111,?projection='polar')

bars?=?ax.bar(theta,?radii,?width=width,?bottom=0.0)

#?Use?custom?colors?and?opacity

for?r,?bar?in?zip(radii,?bars):

????bar.set_facecolor(plt.cm.jet(r?/?10.))

????bar.set_alpha(0.5)

plt.show()

差不多上面代码的原理,具体的自己照着官方文档改

(责任编辑:IT教学网)

更多

推荐其它系统文章