Create a vertical bar chart

Assignment Help Python Programming
Reference no: EM133087573

Lab - Libraries and Graphing Data

Exercises
Question 1. In a new, empty .pyfIle, import the Fraction module (from fractions import Fraction) and then create a Fraction object equal to one-half (i.e. ½).

Then, using a loop - add to this fraction the next 10 values in the sequence 1/4, 1/8, 1/16 etc. Print the new value of the fraction after the addition at each step of the loop.

If you kept on going, would the value of your fraction ever reach 1? Try changing the loop to a bigger value to find out!

Question 2. In a new, empty .pyfile, write a program that imports the plot and show functions frommatplotlib (from pylab import plot, show) and then import Pythons random module, which is part of the standard library, via a the line: import random as rnd

Next, create a list of values called x_valueswhich holds a list of 10 values going from 0 through to 9 - these will be the horizontal values in our graph.

Now create a list of 10 random values within the range 1 to 100 called y_values. You can generate a random value like this: rnd.randint(1, 100). Don't forget you can use the append function on a list to add a new value to the end of it!
Once you have 10 values each in your x_valuesand y_valueslists, you can plot them like this:

show( plot(x_values, y_values) )

Run your program a few times and you will see different graphs each time because of the random values used on the y-axis, for example:

Experimentation: Try increasing the number of plotted points from 10 to 100 or so. Try changing the range of random values down to just two values (0 and 1), or try a large possible range.

Question 3. In a new, empty .pyfile import the following functions from the pyplotmodule:
- plot,
- show,
- title,
- xlabel, and
- ylabel.
You are provided with the following 12 values which are the share price for Microsoft in US dollars at the end of each month from January to December 2017.
msft_share_prices = [62.14, 64.65, 63.98, 65.65, 68.46, 69.84, 68.17, 72.58, 74.77, 83.18, 84.17, 85.54]
Create a list of 12 values representing the months (1 through 12 inclusive) and plot the month number on the x-axis against the share price on the y-axis. Your plot should look like this:

Using, the title function - give the plot a title of "Microsoft Share Price 2017".Using the xlabelfunction give the horizontal axis a label of "Month Number". And using the ylabelfunction give the vertical axis a label of "Price (USD)".Your graph should now look like this:

The price of Apple shares (the company, not the fruit!) is given below:

aapl_share_prices = [136.99, 143.66, 143.65, 152.76, 144.02, 148.73, 164.00, 154.12, 169.04, 171.85, 169.23, 167.43]

Add this second plot to your graph and change the title to read "Microsoft Vs. Apple Share Prices 2017". Your graph should now look like this:

We know that the new line added is the price of Apple shares - but people who look at the graph will not know straight away which one is which - so we need to add a legend to our graph! To do this, add the legend function to list of imports at the top of your code and then modify your plot lines of code for both sets of data to be like this:

plot(msft_share_prices, months, label='MSFT')
plot(aapl_share_prices, months, label='AAPL')

Now, the final thing to do is just call legend() on its own and the legend will be added to the graph, which should now look like this:

Take a look at the above link to find some different ones to experiment with.
The final thing we'll do is add a grid and align it to round numbers. To do this add gridand ylimto your list of function imports at the top of your code, then add the following code before you display the graph:

Question 4. Plot the following graph using 2,000 generated values:

# Calculate a step value for each set of x and y values.
# Note: The value 5.0 is related to the number of ‘nodes' you'll see in the graph - try changing this value when it's working!
step = float(index / number_of_values) * 5.0 * 3.14159

x[index] = step - 1.6 * cos(24 * step)
y[index] = step - 1.6 * sin(25 * step)

To use the cos and sin functions you'll need to import the math module and reference them as math.cosand math.sin. Your final graph should look like this:

Question 5. Create a vertical (i.e. ‘standard') bar chart which displays Formula 1 driver Lewis Hamilton's final season positions for the last 10 years. Below is a table of his finishing positions:

Take a look at slides 54 and 55 if you need a reminder on barcharts.Once done, your bar chart should look like this:

Question 6. Challenge Task: The following are the results of Formula 1 racing driver Fernando Alonso in the years 2017 to 2017:

To complete this task, you'll want to import the numpylibrary, and generate the positions range via np.arangerather than the standard Python range function. This will allow you to set the index of each bar at a location which is the position minus the bar width for the left bar, and the position plus the bar width for the right bar.
Then, you can specify the width attribute of each bar to be twice the bar width, i.e. width=(bar_width * 2).
You can also call plt.grid() to add a grid to the chart to make it easier to read.

Attachment:- Libraries and Graphing Data.rar

Reference no: EM133087573

Questions Cloud

External environmental and competitive factors : Bloomfield Designers is a UK fashion retailer which designs and sells its own brand of women's clothes through its chain of shops in Wales.
Contributions of sex trafficking of teenage girls : What effect does social media have on the contributions of sex trafficking of teenage girls?
Describe what data abstraction is and how it is used : Describe what data abstraction is and how it is used. How is data abstraction different from process abstraction
Social media and digital communication : The concept of libel is an important one given today's use of social media and digital communication. Brief description of libel law.
Create a vertical bar chart : Create a vertical bar chart which displays Formula 1 driver Lewis Hamilton's final season positions for the last 10 years.
Diversity and equity : Write a post evaluating these proposed ethical news values for their usefulness. Could these new news values really be followed in journalism?
Concept of victim impact statements : Discuss the concept of victim impact statements and the impact that they have on victims, as well as, the defendant.
Crime victims : What rights does the state that you reside in provide to crime victims? What rights do you believe to be the most important?
What consideration would you bring to legal process : What consideration would you bring to the legal process (Miranda Rights)? Is there a specific way to conduct the interview,

Reviews

Write a Review

Python Programming Questions & Answers

  Implement the remaining simple arithmetic operators

Implement the remaining simple arithmetic operators (_sub__, __mul __ and truediv ___). Implement the remaining relational operators.

  Determine the page which has the earliest edit time

In this problem you will think of, and execute, series of queries on your table data from problem 1 of first part. Determine page which has earliest edit time

  Analyse the data using statistical and machine learning

analyse the data using statistical and machine learning techniques and produce - a classification algorithm for predicting which customers will be big spenders

  Calculate the sum of each value of n squared

Calculate the sum of each value of N squared from 0 up to and including N. The response paper should be in APA format, double spaced, hand-written.

  Simulate a game of rock-paper-scissors

Write a program that utilizes functions to simulate a game of rock-paper-scissors. Write a program that allows the user to play Rock, Paper, Scissors.

  Price prediction using machine learning approaches

Used Car Price Prediction using Machine Learning Approaches in Various Algorithms Models (Logistic Regression, Decision Tree, Random Forest, SVM, Naïve Bayes

  Define your except blocks before the try block

What keyword do we use to specify a block of code that will handle an exception? Define your except blocks before the try block.

  Write a program that ask the user for the price of an item

Write a program that ask the user for the price of an item, and then prints out a bill for the item. The bill includes the item price, the sales tax on the item

  Write a program in python that reads in x whole numbers

Write a program in python that reads in X whole numbers and outputs the sum of all positive numbers, the sum of all negative numbers.

  Write a program that prints the longest substring

HIT137 SOFTWARE NOW - Charles Darwin University - Write a program that prints the longest substring - Write a python program that allows the user to navigate

  Compare crimes rates in different cities

Use the following dataset 'Crime in Major Cites' to compare crimes rates in different cities for the years listed in the dataset. Is there an obvious trend

  Calculate the correlation in daily adjusted close and volume

Develop a new variable return, defined as the percentage change of a day's Adjusted Close compared to last trading day's Adjusted Close.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd