Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The following question pertains to Python. Consider the following Python bar chart script below. Modify the bar chart script to read data from a file

The following question pertains to Python.

Consider the following Python bar chart script below.

image text in transcribed

Modify the bar chart script to read data from a file called "names.txt" by doing the following:

  1. Modify the Python script by adding code that reads in the names and ages file into two arrays: "names" and "ages."
  2. Erase the fruits and counts arrays from the original script.
  3. Pass the names and ages arrays to the figure() and vbar() functions instead of the fruits and counts arrays.
  4. In the figure() function, change the title of the chart from Fruit Counts to Person Ages.

Notice that modifying the python script to read the names and ages is by adding lines of code to perform these steps:

  1. Open the names.txt file in reading mode.
  2. Call the readline() function to read the header line and ignore it.
  3. Create two empty arrays, names=[] and ages = []
  4. Create a for loop that scans the file line by line. For each line:
    1. Split it into name and age using the split() function and the \t tab delimiter.
    2. Add the name to the names array and the age to the ages array using the append() function.
  5. Close the names.txt file.
from bokeh.io import show, output_file from bokeh.plotting import figure output_file("bar_basic.html") fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries'] counts = [5, 3, 4, 2, 4, 6] p = figure(x_range=fruits, plot_height=350, title="Fruit Counts", toolbar_location=None, tools="") p.vbar(x=fruits, top=counts, width=0.9) p.xgrid.grid_line_color = None p.y_range. start = 0 show(p)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Management An Organizational Perspective

Authors: Richard T. Watson

1st Edition

0471305340, 978-0471305347

More Books

Students also viewed these Databases questions

Question

Writing a Strong Introduction

Answered: 1 week ago