Pygal
Pygal can be used to generate scalable vector graphics files, which are useful for charts that need to be displayed on different sizes of screens, and can be automatically scaled to fit the viewer's screen
1. Pygal Module Installation
① Windows system
# Pygal Module Installation $ PIP3 Install Pygal
② Linux and OS X systems
# Pygal Module Installation $ pip install--user pygal==1.7
2. Pygal Gallery-Histogram
Simulate roll dice, analyze final results, generate graphs
To create a die.py sieve class file:
From the random import Randintclass die (): "Throw the Dice Class" def __init__ (self,num_sides=6): self.num_sides = num_ Sides # Number of faces in Dice def roll (self): return Randint (1,self.num_sides)
Create a die_visual.py file to generate a histogram:
From die import dieimport Pygaldie = Die () # Data Set results = []count = 1for roll_num in iter (Lambda *args:die.roll (), None):
results.append (roll_num) if Count >=: Break count +=1# analysis result frequencies= []for value in range (1, die.num_sides+1): Frequencie = results.count (value) frequencies.append (frequencie) # visualize the result hist = Pygal. Bar () # Build Instance hist.title = ' Results of rolling one D6 Times ' # title hist.x_labels = [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 '] # x Axis numeric coordinate hist.x_title = ' result ' # x axis Title Hist.y_title = ' Frequency of result ' # y-axis title hist.add (' D6 ', frequencies) c11/># Incoming y-axis data hist.render_to_file (' Die_visual.svg ') # file generation path, must be an SVG format file
To open the Die_visual.svg file via the browser:
3. Throw two dice at the same time
To modify the die_visual.py file:
From die import dieimport Pygaldie1 = die () Die2 = Die () # Data Set results = []for i in range: result = Die1.roll () + Die2.roll () results.append (Result) # parse result frequencies= []for value in range (2,die1.num_sides+die2.num_sides+1): Frequencie = results.count (value) frequencies.append (frequencie) # visualize the result hist = Pygal. Bar () # Build Instance hist.title = ' Results of rolling one D6 Times ' # title hist.x_labels = [' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' Ten ', 11,12] # x Axis numeric coordinates hist.x_title = ' Result ' # x axis Title Hist.y_title = ' Frequency of Result ' # y-axis title Hist.add ( ' D6+d6 ', frequencies) # incoming y-axis data hist.render_to_file (' Die_visual.svg ') # file generation path, must be an SVG format file
Browser Browsing Graphics die_visual.svg:
Python development "Modules": Pygal drawing histograms