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

  Write a python program to implement the diff command

Without using the system() function to call any bash commands, write a python program that will implement a simple version of the diff command.

  Write a program for checking a circle

Write a program for checking a circle program must either print "is a circle: YES" or "is a circle: NO", appropriately.

  Prepare a python program

Prepare a Python program which evaluates how many stuck numbers there are in a range of integers. The range will be input as two command-line arguments.

  Python atm program to enter account number

Write a simple Python ATM program. Ask user to enter their account number, and print their initail balance. (Just make one up). Ask them if they wish to make deposit or withdrawal.

  Python function to calculate two roots

Write a Python function main() to calculate two roots. You must input a,b and c from keyboard, and then print two roots. Suppose the discriminant D= b2-4ac is positive.

  Design program that asks user to enter amount in python

IN Python Design a program that asks the user to enter the amount that he or she has budget in a month. A loop should then prompt the user to enter his or her expenses for the month.

  Write python program which imports three dictionaries

Write a Python program called hours.py which imports three dictionaries, and uses the data in them to calculate how many hours each person has spent in the lab.

  Write python program to create factors of numbers

Write down a python program which takes two numbers and creates the factors of both numbers and displays the greatest common factor.

  Email spam filter

Analyze the emails and predict whether the mail is a spam or not a spam - Create a training file and copy the text of several mails and spams in to it And create a test set identical to the training set but with different examples.

  Improve the readability and structural design of the code

Improve the readability and structural design of the code by improving the function names, variables, and loops, as well as whitespace. Move functions close to related functions or blocks of code related to your organised code.

  Create a simple and responsive gui

Please use primarily PHP or Python to solve the exercise and create a simple and responsive GUI, using HTML, CSS and JavaScript.Do not use a database.

  The program is to print the time

The program is to print the time in seconds that the iterative version takes, the time in seconds that the recursive version takes, and the difference between the times.

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