Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to draw a simple crease figure with Python

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to draw a simple crease diagram with Python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Installation environment matplotlib

Personally, I also said earlier that it is strongly recommended to use Pycharm as the preferred IDE for Python beginners, mainly because of its powerful plug-in features, many environments can be installed with one click, such as matplotlib,numpy,requests in this article.

The following is directly on the effect picture:

Draw a simple line drawing using plot to draw a line import matplotlib.pyplot as plt# draw a line chart squares = [1, 4, 9, 16, 25] # plt.plot (squares, linewidth=5) # specify the weight of the line, # # plt.show () # modify label text and line weight # plt.title ("squre number", fontsize=24) # plt.xlabel ("Value", fontsize=14) # plt.ylabel ("square of value", fontsize=14) # plt.tick_params (axis='both', labelsize=14) # plt.show () # Correction graphic input_values = [1,2,3,4,5] plt.plot (input_values, squares, linewidth=5) plt.show ()

The resulting effect picture:

Use scatter to draw scatter plots and style import matplotlib.pyplot as plt# simple dots # plt.scatter (2,4) # plt.show () # modify label text and line weight plt.title ("squre number", fontsize=24) plt.xlabel ("Value", fontsize=14) plt.ylabel ("square of value", fontsize=14) # set the scale mark size plt.tick _ params (axis='both', which='major', labelsize=14) # draw scatter x_values = [1,2 3, 4, 5] y_values = [1, 4, 9, 16, 25] plt.scatter (x_values, y_values, 100) plt.show ()

Automatically calculate data import matplotlib.pyplot as pltx_values = list (range (1, 1001)) y_values = [x * * 2 for x in x_values] # y_values = [x * x for x in x_values] # y_values = [x ^ 2 for x in x_values] plt.scatter (x_values, y_values, swarm 40) # the value range of the axis # plt.axis (0, 1100, 0, 1100000) # is xmin xmax,ymin,ymaxplt.show ()

Random walk import matplotlib.pyplot as plyfrom random import choiceclass RandomWalk (): def _ init__ (self, num_points=5000): self.num_points = num_points self.x_values = [0] self.y_values = [0] def fill_walk (self): # keep walking Until the specified number of steps is reached while len (self.x_values) < self.num_points: # determines the direction of progress and the distance x_direction = choice ([1,-1]) x_distance = choice ([0,1,2,3,4,5,6,7,8) 9]) x_step = x_direction * x_distance y_direction = choice ([1,-1]) y_distance = choice ([0,1,2,3,4,5,6,7,8) 9]) y_step = y_direction * y_distance # cannot stand still 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 self.x_values.append (next _ x) self.y_values.append (next_y) rw = RandomWalk () rw.fill_walk () ply.scatter (rw.x_values Rw.y_values, 15) ply.show () effect picture

Use Pygal to simulate rolling dice

The graphics that can be drawn by pygal can be accessed by pygal introduction.

Environment installation, install plug-ins directly on Pycharm.

Import pygalfrom random import randintclass Die (): def _ init__ (self, num_sides=6): self.num_sides = num_sides; def roll (self): # returns a random value between 1 and the number of dice faces return randint (1, self.num_sides) die = Die () results = [] # roll the dice 100 times and put the result in the list. For roll_num in range (10): result = die.roll () results.append (str (result)) print (results) # Analysis results frequencies = [] for value in range (1, die.num_sides + 1): frequency = results.count (value) frequencies.append (frequency) print (frequencies) # Visualize the results hist = pygal.Box () hist.title = "result of rolling one D6 1000 times" hist.x_labels = ['1th,' 2' Hist.x_title = "Result" hist.y_title = "frequency of result" hist.add ('D6mom, frequencies) hist.render_to_file (' die_visual.svg') install requests using Web API1.1

This plugin can be installed directly in Pycharm, which is very convenient.

1.2 process API response import requests# execute api call and store response url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'r = requests.get (url) print ("Status code:", r.status_code) # store api response in a variable response_dic = r.json () # processing result print (response_dic.keys ()) get the result: Status code: 200dict_keys ([' total_count', 'incomplete_results'') 'items']) 1.3 processing response dictionary # stores the api response in a variable response_dic = r.json () # processing result print (response_dic.keys ()) print ("Total repositories:", response_dic [' total_count']) repo_dics = response_dic ['items'] print ("repositories returned:" + str (len (repo_dics) # Research a warehouse repo_dic = repo_dics [0] print ("\ nKeys:" Str (len (repo_dic)) # for key in sorted (repo_dic.keys ()): # print (key) print ("Name:", repo_dic ['name']) print ("Owner:", repo_dic [' owner'] ['login']) print ("Starts:", repo_dic [' stargazers_count']) print ("Repository:", repo_dic ['html_url']) print ("Created:", repo_dic [' created_at']) print ("Updated:" Repo_dic ['updated_at']) print ("Description:", repo_dic [' description']) get the result: Total repositories: 2061622repositories returned:30Keys: 71Name: awesome-pythonOwner: vintaStarts: 40294Repository: https://github.com/vinta/awesome-pythonCreated: 2014-06-27T21:00:06ZUpdated: 2017-10-29T00:50:49ZDescription: A curated list of awesome Python frameworks, libraries, software and resources, this is the end of the introduction of "how to draw a simple folding figure with Python". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report