Using PyPI in Python

Source: Internet
Author: User

Install the Pygal plug-in using the PIP install Pygal in the console. Reference URL: http://www.pygal.org/en/stable/installing.html


2. In the die.py document,

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
3. In the die_visual.py file
Import Pygal from
die import die
# Create a D6
die = Die ()

# Roll several dice and store the results in a list
results = [] for
Roll_ Num in range (1000):
 outcome = Die.roll ()
 results.append (Result)
 # parse Results
frequencies = [] for
value In range (1, die.num_sides+1):
 frequency = Results.count (value)
 frequencies.append (frequency)
print ( frequencies)

# Visualize the result
hist = Pygal. Bar ()
hist.title = "Results 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)
#将结果导出到一个svg文件中, in the browser can preview the view
hist.render_to_file (' die_visual.svg ')
  

4. The code run produces die_visual.svg files, which are opened in the browser as follows.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.