程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python data visualization ----- generate data 2

編輯:Python

使用Plotly模擬擲骰子:

安裝Plotly:

在終端輸入pip install plotly直接進行安裝

To simulate rolling dice,We must first create a class about dice.

創建Die類:

language_survey.py

from random import randint#導入隨機數的庫
class Die:#創建一個表示骰子的類
def __init__(self,num_sides=6):#Set the properties of the dice,for six-sided dice
self.num_sides=num_sides
def roll(self):
return randint(1,self.num_sides)#返回一個1-6范圍內的隨機數

擲骰子:

代碼如下:

project1.py

from language_survey import Die#Import the class we wrote about dice in another file
die=Die()#使用實例調用
results=[]#Store the different results produced in a list
for roll_num in range(100):
result=die.roll()
results.append(result)
print(results)
[6, 6, 3, 5, 6, 3, 3, 3, 4, 6, 6, 5, 3, 5, 5, 6, 2, 3, 6, 4, 4, 6, 5,
5, 3, 6, 3, 3, 2, 3, 5, 4, 6, 2, 5, 3, 4, 1, 5, 3, 4, 6, 6, 4, 4, 2,
4, 2, 5, 4, 4, 1, 3, 5, 1, 3, 5, 2, 4, 5, 6, 1, 1, 4, 4, 6, 5, 3, 6,
5, 6, 5, 3, 1, 6, 4, 1, 5, 6, 1, 1, 3, 5, 6, 3, 4, 5, 4, 2, 5, 6, 4,
1, 5, 1, 1, 6, 2, 6, 2]

The output seems to be fine,The output numbers are in the range0-6之間.

If we want to record throwsN次之後,The number of times a point occurs without a specific result?
,很簡單,We can store the number of times a certain point of the throw occurs by creating an empty list again.

Add the following code to the above example:

frequencies=[]
for value in range(1,die.num_sides+1):
frequency=results.count(value)
frequencies.append(frequency)
print(frequencies)
[15, 10, 23, 12, 17, 23]

繪制直方圖:

直方圖是一種條形圖,The frequency of occurrence of various results is indicated,The code to create such a histogram is as follows:

#對結果進行可視化
x_values=list(range(1,die.num_sides+1))#Store the number of possible occurrences in a named x_values的列表中
#注:Plotly不能直接接受函數range()的結果,因此需要使用函數list將其轉換為列表
data=[Bar(x=x_values,y=frequencies)]#Bar()表示用於繪制條形圖的數據集,need a storexWorth a list and a storey值的列表
#注:PlotlyClasses must be enclosed in square brackets,Because the dataset may contain multiple elements
#Set labels for the horizontal and vertical axes
x_axis_config={
'title':'結果'}
y_axis_cofig={
'title':"結果的頻率"}
#類Layout()Set the layout and title of the chart
my_layout=Layout(title="Roll a six-sided die1000次的結果:",xaxis=x_axis_config,yaxis=y_axis_cofig)
#調用offline.plot()in order to generate the chart
#offline.plot()The function expects a dictionary containing data and layout objects,And also need to pass in a file name,The chart will then be placed in this file
offline.plot({
'data':data,'layout':my_layout},filename='d6.html')
print(frequencies)

運行程序後:

output result:

[10, 20, 15, 17, 20, 18]

Automatically jump to the browser,Renders the histogram as shown in the image below:
注意,PlotlyMake the chart interactive,This interactivity occurs when we point the mouse to any of the bars in it,就能看到與之相關聯的數據.
And don't ignore these icons in the upper left corner,It also has many practical functions,For example, it can help us zoom, pan, and save images.

Roll two dice with the same number of faces at the same time:

Earlier we learned that throwing a dice requires creating a class related to it,Now we also need to create its own class for the second die,The numbers of the two dice are added each time they are rolled,並將其存儲在results中.

The code that needs to be modified is as follows:

---snip---
die_2=Die()#Create the class that belongs to the second dice
results=[]
for roll_num in range(100):
result=die_1.roll()+die_2.roll()#The result returned is the sum of the two dice
results.append(result)
frequencies=[]
max_result=die_1.num_sides+die_2.num_sides#Sum of maximum points
for value in range(2,max_result+1):#Possible values ​​for the sum of points
frequency=results.count(value)
frequencies.append(frequency)
x_values=list(range(2,max_result+1))
data=[Bar(x=x_values,y=frequencies)]
x_axis_config={
'title':'結果',"dtick":1}#ditick的作用:指定x軸顯示的刻度間距
y_axis_cofig={
'title':"結果的頻率"}
my_layout=Layout(title="Roll a six-sided die100次的結果:",xaxis=x_axis_config,yaxis=y_axis_cofig)
offline.plot({
'data':data,'layout':my_layout},filename='d6_d6.html')
print(frequencies)

運行之後結果為:

output result:

[7, 3, 8, 11, 9, 16, 17, 14, 6, 5, 4]

Automatically jump to the browser,Renders the histogram as shown in the image below:

Throw two dice with different faces at the same time:

Just pass the parameter as when creating a new class10,將默認值6覆蓋,Then the range of times the dice are thrown is changed.

代碼如下:

from plotly.graph_objs import Bar,Layout
from plotly import offline
from language_survey import Die
die_1=Die()
die_2=Die(10)#創建10面的骰子
results=[]
for roll_num in range(50_000):#Change the number of throws
result=die_1.roll()+die_2.roll()
results.append(result)
frequencies=[]
max_result=die_1.num_sides+die_2.num_sides
for value in range(2,max_result+1):
frequency=results.count(value)
frequencies.append(frequency)
x_values=list(range(2,max_result+1))
data=[Bar(x=x_values,y=frequencies)]
x_axis_config={
'title':'結果',"dtick":1}
y_axis_cofig={
'title':"結果的頻率"}
my_layout=Layout(title="Roll a six- and ten-sided die50_000次的結果:",xaxis=x_axis_config,yaxis=y_axis_cofig)
offline.plot({
'data':data,'layout':my_layout},filename='d6_d10.html')
print(frequencies)

運行之後結果為:

output result:

[816, 1676, 2568, 3351, 4217, 5061, 4977, 5041, 4870, 5043, 4167, 3309, 2432, 1671, 801]

Automatically jump to the browser,Renders the histogram as shown in the image below:


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved