# Install plot library using pip
import sys
!{sys.executable} -m pip install matplotlib
# Move to the plot directory
%cd ../fields/graphs
/dave/software/codeplayfield/fields/graphs
The following values are some coins. The kinds of coins or amounts are pennies=1, nickes=5, dimes=10, and quarters = 15. They were added to the coinAmounts list as they were picked up.
from LibGraph.Graph import *
coinAmounts = [5, 1, 1, 10, 1, 25, 1, 1, 10, 25, 5]
displayLineGraph1D(coinAmounts, 'x', 'y')
The x and y labels are correct, but they don't mean much. There were 11 coins, so the x axis shows that there are 11 items numbered 0 to 10. The y axis is 0 to 25, so that is the coin amount. So improving the meaning of the labels gives the following.
This graph makes it easy to see that there are two coins that are 25 cents, and 1 coin that is 10 cents. How many 5 cent values are there? The number of 1 cent values is more difficult to count, because there are starting and ending points that all must be counted. For example, there is a 1 cent piece at index 1, 2 and some others.
coinAmounts = [5, 1, 1, 10, 1, 25, 1, 1, 10, 25, 5]
displayLineGraph1D(coinAmounts, 'Coin Index', 'Coin Amounts')
To make it easier to count the coins, each coin can be shown as a point. Notice that the amounts are in the same place, but lines are not connecting them.
coinAmounts = [5, 1, 1, 10, 1, 25, 1, 1, 10, 25, 5]
displayPointsGraph1D(coinAmounts, 'Coin Index', 'Coin Amounts')
Python has an easy way to sort the values. This makes it easier to add the amounts together.
coinAmounts = [5, 1, 1, 10, 1, 25, 1, 1, 10, 25, 5]
sortedCoinAmounts = sorted(coinAmounts)
displayPointsGraph1D(sortedCoinAmounts, 'Coin Index', 'Coin Amounts')
print('sum of coins', sum(coinAmounts))
sum of coins 85
There is a different kind of graph that can show the count of each kind of coin. This graph shows that there are 5 one cent values, 2 five cent values, etc.
coinAmounts = [5, 1, 1, 10, 1, 25, 1, 1, 10, 25, 5]
displayHistogram(coinAmounts, 'Coin Amount', 'Number of Coins')
The "bins" are figured out automatically, but they don't work well for coins. In this case, it looks like the bins are about 2.5 wide.
The histogram works by figuring out which values go into each bin. For the following example, the bins are defined. The values between 0.5 and 1.5 will go into the second bin. Then there are zero values that are between 1.5 and 4.5 because the coin amounts jump from 1 cents to 5 cents.
coinAmounts = [5, 1, 1, 10, 1, 25, 1, 1, 10, 25, 5]
bins = [0, 0.5, 1.5, 4.5, 5.5, 9.5, 10.5, 24.5, 25.5]
displayHistogram(coinAmounts, 'Coin Amount', 'Number of Coins', bins)
The first value in each pair (tuple) is the coin amount, then second value is the year it was made.
from LibGraph.Graph import *
coins = [
(5, 2004), (1, 2001), (1, 1990), (10, 2020), (1, 1990), (25, 1999), (1, 2003),
(1, 2014), (10, 2016), (25, 1979), (5, 2015), (1, 2016), (1, 2020), (1, 2012),
(25, 2002), (1, 1976), (25, 1989), (25, 2013), (1, 2012), (1, 1983), (1, 2012),
(25, 2018), (1, 2019), (1, 1978), (1, 2019), (1, 1998), (25, 2006), (25, 2019),
(1, 2020), (25, 1990), (1, 2004), (1, 2015)]
coinAmounts = []
for pair in coins:
coinAmounts.append(pair[0]) # Get the first value (index=0) of every pair
displayLineGraph1D(coinAmounts, 'x', 'y')
Instead of using 3 lines to get the amounts of the coins, the following line could be used.
coinAmounts = [coin[0] for coin in coins]