Tuesday, September 24, 2019

Using Plotly to produce interactive visualizations

Plotly is particularly useful when you’re creating visualizations that will be displayed in a browser, because the visualizations will scale automatically to fit the viewer’s screen. Visualizations that Plotly generates are also interactive; when the user hovers over certain elements on the screen, information about that element is highlighted.

Install Plotly using pip command pip install plotly

Now we’ll use the Python package Plotly to produce interactive visualizations. We’ll analyze the results of rolling dice. When you roll one regular, six-sided die, you have an equal chance of rolling any of the numbers from 1 through 6. However, when you use two dice, you’re more likely to roll certain numbers rather than others. We’ll try to determine which numbers are most likely to occur by generating a data set that represents rolling dice. Then we’ll plot the results of a large number of rolls to determine which results are more likely than others.

The study of rolling dice is often used in mathematics to explain various types of data analysis. But it also has real-world applications in casinos and other gambling scenarios, as well as in the way games like Monopoly and many role-playing games are played.

First we'll create the following Die class to simulate the roll of one die:

from random import randint

class Die:
    """A class representing a single die."""
   
    def __init__(self,num_sides=6):
        """Assume a six-sided die."""
        self.num_sides = num_sides
       
    def roll(self):
        """"Return a random value between 1 and number of sides."""
        return randint(1, self.num_sides)


The __init__() method takes one optional argument. With the Die class, when an instance of our die is created, the number of sides will always be six if no argument is included. If an argument is included, that value will set the number of sides on the die. The roll() method uses the randint() function to return a random number between 1 and the number of sides i.e this function can return the starting value (1), the ending value (num_sides), or any integer between the two. 

Now let's roll a die D6 and print the results as shown in the following program:

from die import Die

# Create a D6.
die = Die()

# Make some rolls, and store results in a list.
results = []

for roll_num in range(100):
    result = die.roll()
    results.append(result)
   
print(results)

In the program we create an instance of Die with the default six sides. Then we roll the die 100 times and store the results of each roll in the list results as shown below:

[6, 3, 3, 1, 4, 4, 3, 6, 2, 1, 1, 2, 5, 3, 2, 3, 1, 4, 1, 1, 1, 6, 6, 2, 1, 2, 6
, 2, 3, 4, 2, 4, 1, 5, 1, 5, 4, 4, 4, 5, 1, 5, 1, 3, 2, 4, 1, 6, 1, 1, 2, 5, 4,
4, 4, 5, 1, 6, 1, 4, 1, 2, 3, 2, 1, 6, 5, 6, 5, 4, 5, 2, 3, 5, 5, 5, 5, 5, 1, 1,
 4, 2, 4, 1, 3, 1, 3, 1, 5, 3, 2, 6, 4, 6, 2, 4, 4, 4, 1, 6]

------------------
(program exited with code: 0)

Press any key to continue . . .

The results shows that the Die class seems to be working. We see the values 1 and 6, so we know the smallest and largest possible values are being returned, and because we don’t see 0 or 7, we know all
the results are in the appropriate range. We also see each number from 1 through 6, which indicates that all possible outcomes are represented.

Now analyze the results of rolling one D6 by counting how many times we roll each number. See the following code:

from die import Die

# Create a D6.
die = Die()

# Make some rolls, and store results in a list.
results = []

for roll_num in range(1000):
    result = die.roll()
    results.append(result)

# Analyze the results.
frequencies = []
for value in range(1, die.num_sides+1):
    frequency = results.count(value)
    frequencies.append(frequency)
       
print(frequencies)


As we’re no longer printing the results, we can increase the number of simulated rolls to 1000. To analyze the rolls, we create the empty list frequencies to store the number of times each value is rolled. We loop through the possible values (1 through 6 in this case), count how many times each number appears in results, and then append this value to the frequencies list. We then print this list before making a visualization:

[138, 175, 172, 161, 179, 175]
------------------
(program exited with code: 0)

Press any key to continue . . .

In the results we see six frequencies, one for each possible number when you roll a D6, and we see that no frequency is significantly higher than any other. Now let’s visualize these results, with a list of frequencies, we can make a histogram of the results. See the following code:

from die import Die
from plotly.graph_objs import Bar, Layout
from plotly import offline

# Create a D6.
die = Die()

# Make some rolls, and store results in a list.
results = []

for roll_num in range(1000):
    result = die.roll()
    results.append(result)

# Analyze the results.
frequencies = []
for value in range(1, die.num_sides+1):
    frequency = results.count(value)
    frequencies.append(frequency)
       
# Visualize the results.

x_values = list(range(1, die.num_sides+1))
data = [Bar(x=x_values, y=frequencies)]
x_axis_config = {'title': 'Result'}
y_axis_config = {'title': 'Frequency of Result'}

my_layout = Layout(title='Results of rolling one D6 1000 times',xaxis=x_axis_config, yaxis=y_axis_config)
offline.plot({'data': data, 'layout': my_layout}, filename='d6.html')


To make a histogram, we need a bar for each of the possible results. We store these in a list called x_values, which starts at 1 and ends at the number of sides on the die. Plotly doesn’t accept the results of the range() function directly, so we need to convert the range to a list explicitly using the list() function. The Plotly class Bar() represents a data set that will be formatted as a bar chart. This class needs a list of x-values, and a list of y-values. The class must be wrapped in square brackets, because a data set can have multiple elements.

Each axis can be configured in a number of ways, and each configuration option is stored as an entry in a dictionary. At this point, we’re just setting the title of each axis. The Layout() class returns an object that specifies the layout and configuration of the graph as a whole. Here we set the title of the graph and pass the x- and y-axis configuration dictionaries as well.

To generate the plot, we call the offline.plot() function. This function needs a dictionary containing the data and layout objects, and it also accepts a name for the file where the graph will be saved. We store the output in a file called d6.html. When we run the program die_visual.py, a browser will probably open showing the file d6.html as shown below:



Notice that Plotly has made the chart interactive: hover your cursor over any bar in the chart, and you’ll see the associated data. This feature is particularly useful when you’re plotting multiple data sets on the same chart. Also notice the icons in the upper right, which allow you to pan and zoom the visualization, and save your visualization as an image.

Here I am ending today's post. In the next post we'll see how to roll two dice.










Share:

0 comments:

Post a Comment