Create a bar chart with two bars side

Assignment Help Python Programming
Reference no: EM133091537

Libraries and Graphing Data

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:

390_Graphing Data.jpg

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:

1794_Graphing Data1.jpg

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:

183_Graphing Data3.jpg

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:

2134_Graphing Data2.jpg

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:

2428_Graphing Data4.jpg

Another thing we could do is to add some markers to this graph. There are lots of different marker types we could use and you can add a marker by simple adding a section to your plot statements like marker='x' or marker='o'. When you've done this, your graph should now look like this:

 

344_Graphing Data5.jpg

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:

msft_min = min(msft_share_prices) # Get minimum Microsoft share price
aapl_min = min(aapl_share_prices) # Get minimum Apple share price
grid_min = min(msft_min, aapl_min) # Get minimum of both of these values
grid_min -= grid_min % 10 # Round down to nearest 10 dollars
ylim(ymin = grid_min) # Apply ymin value to grid y-axis

With this done, our final graph should look like this:

1076_Graphing Data6.jpg

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:

1087_Graphing Data7.jpg

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:

Year

2007

2008

2009

2010

2011

2012

2013

2014

2015

2016

2017

Final Position

2

1

5

4

5

4

4

1

1

2

1

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

769_Graphing Data8.jpg

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

Year

2007

2008

2009

2010

2011

2012

2013

2014

2015

2016

2017

Final Position

3

5

9

2

4

2

2

6

17

10

15

Create a bar chart with two bars side-by-side for each year. Take a look at this to see how it's done:

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.

59_Graphing Data9.jpg

Attachment:- Libraries and Graphing Data.rar

Reference no: EM133091537

Questions Cloud

Create a company-wide planning culture : Analysis tool as a means to create a company-wide planning culture? Do you believe it would be helpful for the company's sustained competitiveness? Why, or why
When the value of a currency rises : When the value of a currency rises due to demand and supply factors, is it correct to say that the currency has undergone revaluation?
Research videos and database of business sources : You might use a database of Company and Research Videos and database of Business Sources as a marketing manager within a company
What is the total cost of the goods transferred : During the month, 2,100 units of a product were completed and transferred to finished goods. What is the TOTAL cost of the goods transferred
Create a bar chart with two bars side : Create a bar chart with two bars side-by-side for each year and Create a vertical (i.e. ‘standard') bar chart which displays Formula 1 driver Lewis Hamiltons
Determine fiber company break-even in sales dollars : Fiber Company sells several products. Information of revenue and costs is as follows: Selling price per unit $28.50. Determine Fiber Company break-even
Level of communication environment : 1) ABC Company is about to announce their acquisition of another company. Most employees expect this announcement as there have been numerous rumor's to this ef
Discuss the situation from strictly legal viewpoint : You are the general manager of a regional chemical company. In the course of producing your bulk chemicals, large amounts of particles and smoke are emitted thr
What is the firm after-tax weighted average cost of capital : Company XYZ Ltd has debt with a market value of $4.4 billion and equity market value of $71.4 billion. What is firm after-tax weighted average cost of capital

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