study and use of Matplotlib
installation of Matplotlib
PIP3 Install Matplotlib
a simple line chart
Import Matplotlib.pyplot as Plt
#绘制简单的图表
input_values = [1,2,3,4,5]
squares = [1,4,9,16,25]
Plt.plot (input_values,squares,linewidth=5)
#设置图表的标题 and label
the axes plt.title ("Square number", fontsize=24)
Plt.xlabel ("value", fontsize=24)
Plt.ylabel ("Square of Value", fontsize=14)
#设置刻度标记的大小
Plt.tick_params (axis= ' both ', labelsize=14)
#显示图表
plt.show ()
#保存在当前的目录下, file name is Squares_plot.png
#plt. Savefig (' squares_plot.png ', bbox_inches= ' tight ')
draw a simple scatter chart
Import Matplotlib.pyplot as plt
x_values = [1, 2, 3, 4, 5]
y_values = [1, 4, 9,,]
plt.scatter (x_values, Y_values, s=100)
#设置图表的标题 and label the axes
plt.title ("Square number", fontsize=24)
Plt.xlabel ("Value", fontsize=24)
Plt.ylabel ("Square of Value", fontsize=14)
#设置刻度标记的大小
plt.tick_params (axis= ' both '), LABELSIZE=14)
plt.show ()
Import Matplotlib.pyplot as Plt
#绘制散点图并设置其样式
x_value = List (range (1,1001))
y_value = [x**2 for x in X_value]
#点的颜色 c= (0,0,1,0.5) edgecolors = Edge color plt.scatter of ' red ' points
(x_value,y_value,c=y_value,cmap= Plt.cm.blues,edgecolors= ' None ', s=40)
# plt.scatter (2,4,s=200)
#设置图表的标题 and label
the axes Plt.title (" Square number ", fontsize=24)
Plt.xlabel (" value ", fontsize=24)
Plt.ylabel (" Square of Value ", fontsize=14)
#设置刻度标记的大小
plt.tick_params (axis= ' both ', labelsize=14)
#设置每个坐标系的取值范围
# Plt.axis ([ 0,110,0,110000])
#显示
plt.show ()
#显示并保存
#plt. Savefig (' pyplot_scatter.png ', bbox_inches= ') Tight ')
draw a random stroll chart
random_walk.py
From random Import Choice Class Randomwalk (): "" "" "a class that generates random walks of data" "Def __init__ (self,num_points=5000):" "
"A class that generates random walks of data" "self.num_points = num_points;
#所有的随机漫步都始于 (0,0) Self.x_value = [0] Self.y_value = [0] def fill_walk (self): "" "compute random walks containing points" ""
#不断漫步 until the column is expressed to the specified length while Len (Self.x_value) < self.num_points: #决定前进的方向以及沿这个方向前进的距离
x_direction= choice ([1,-1]) x_distance = Choice ([0,1,2,3,4]) X_step = x_direction*x_distance y_direction = Choice ([1,-1]) y_distance = Choice ([0, 1, 2, 3, 4]) Y_step = Y_directi On * y_distance #拒绝原地踏步 if X_step = 0 and Y_step = = 0:continue #计算 The X and Y values of the next point next_x = self.x_value[-1] + x_step next_y = self.y_value[-1] + y_step self. X_value.append (next_x) self.y_value.append (next_y)
rw_visual.py
Import Matplotlib.pyplot as Plt
#引用同级目录下的文件 from
random_walk.random_walk import randomwalk
# Create an instance of Randomwalk and draw the points it contains
RW = Randomwalk ()
rw.fill_walk ()
print ("test")
point_numbers = List ( Range (rw.num_points))
plt.scatter (rw.x_value,rw.y_value,c=point_numbers, cmap=plt.cm.blues,edgecolor= ' None ' , s=15)
# Highlight the starting and ending
plt.scatter (0, 0, c= ' green ', edgecolors= ' none ', s=100)
Plt.scatter (rw.x_value[-1), Rw.y_value[-1],c= ' Red ', edgecolors= ' none ', s=100)
# Set the size of the drawing window
# plt.figure (figsize= (6))
Plt.figure (dpi=128, figsize= (6))
# Hide Axis
# plt.axes (). Get_xaxis (). set_visible (False)
# plt.axes ( ). Get_yaxis (). set_visible (False)
plt.show ()
study and use of Pygal
Install Pygal
PIP3 Install Pygal
draw a simple histogram
Create Dice class die.py
from random import Randint
class Die (): "" ""
is the class of a dice ""
def __init__ (self,num_sides=6):
"" "Dice defaults to 6 sides" ""
self.num_sides = num_sides
def roll (self): ""
returns a random value between 1 and the number of die Faces "" "return Randint (1,self.num_sides)
Roll the dice die_visual.py
From pygal_learn.die import die
import pygal
#创建一个D6
die = Die ()
#掷几次骰子 and store the results in a list
results = [] for
roll_num in range (1000): Outcome
= Die.roll ()
results.append (Result)
frequencies = []
#分析结果 for
value in range (1,die.num_sides+1):
frequency = Results.count (value)
frequencies.append (frequency)
#对结果进行可视化
hist = Pygal. Bar ()
hist.title = "Result of rolling one D6 1000 times"
hist.x_labels = [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ']
hist.x _title = "Result"
Hist.y_title = "Frequency of result"
Hist.add ("D6", frequencies)
Hist.render_to_file ("Die_visual.svg")
using the Web API Install requests
PIP3 Install Requests
Drawing Charts
Draw a chart by grabbing the most popular Python project on the GitHub
Import requests import Pygal from Pygal.style import Lightcolorizedstyle as lcs,lightenstyle as LS #执行API调用并存储响应 URL = ' Https://api.github.com/search/repositories?q=language:python&sort=stars ' r = Requests.get (URL) print ("Staus
Code: ", r.status_code) response_dict = R.json () print (" Total repositories: ", response_dict[' Total_count ']) #探索有关仓库的信息 repo_dicts = response_dict[' Items '] print (' repositories returned: ', Len (repo_dicts)) #研究第一个仓库 # repo_dict = repo_dicts[0 ] # for key in sorted (Repo_dict.keys ()): # Print (key) #研究仓库有关的信息 # name:macos-security-and-privacy-guide # Owner: Drduh # stars:12348 # repository:https://github.com/drduh/macos-security-and-privacy-guide # description:a Practical g
Uide to securing MacOS. Names,plot_dicts = [],[] for repo_dict in Repo_dicts:names.append (repo_dict["Name"]) # Stars.append (repo_dict["St Argazers_count "]) plot_dict = {' value ': repo_dict[' stargazers_count '], ' label ': Str (repo_dict[' DESCRI
Ption ']), ' XLink ': repo_dict[' Html_url ']} plot_dicts.append (plot_dict) #可视化数据 my_config = Pygal. Config () my_config.x_label_rotation = My_config.show_legend = False My_config.title_font_size = My_config.label_ Font_size = My_config.major_label_font_size = My_config.truncate_label = My_config.show_y_guides = False My_confi G.width = 1000 My_style = LS (' #333366 ', Base_style=lcs) chart = Pygal. Bar (my_config,style=my_style) chart.title = "most-stared Python Project on Github" chart.x_labels = names Print (plot_dict s) chart.add (', plot_dicts) chart.render_to_file (' Python_repos.svg ')
rate limit for monitoring APIs
Most APIs have a rate limit, that is, the number of requests you can execute within a specific time limit. To learn if you are close to the GitHub limit, enter Https://api.github.com/rate_limit in the browser and you will see a response similar to the following:
content reference to: "Python programming: Getting Started to practice"