Reference no: EM132158070
Project
Project Objectives
This project requires you to use the graphics module in Python called Turtle to plot a bar chart. You have to compose the chart from basic graphic elements like points, lines and texts using functions in Turtle.
You can jump to the section titled "Step 4 Plot the bar chart of a dataset" for the project requirements.
The project
What you need to produce and submit at the end of this project is a program that plots a bar chart for a given dataset. You are free to design the program in the way you want, so long as it is neat and clear. We will describe the problem later. Here, we first give you a progressive guide on how to do drawings.
You have four weeks for the project. The guide below breaks your progress down to four steps, and you can take one step per week, more or less. If you follow them well, you should be able to produce the desired results at the end. However, we expect that some of you will be able to do it ahead of time.
Step 1 - learn to use Turtle functions
Learn how to use Turtle functions to do some simple drawings. Try these:
- Draw a line, using the direction functions like forward(), backward(), right(), left() etc., with appropriate parameters, of course.
- Draw a line, by specifying coordinates to draw to, using the goto() function, with appropriate control of penup() and pendown().
- Vary the widths of the lines you draw by using pensize().
- Draw multiple lines in different directions, connected and not connected. Try drawing lines to write the word "turtle" or any word you choose.
- Draw a few closed shapes: triangles, rectangles and shapes of your own design, of different sizes, and fill them with colours, especially rectangles, of course, since the bars are rectangular.
- Write some text in the picture.
- Try different colours, for the lines, the textsand the filling of the shapes.
You will see that the default setting of Turtle is to display a triangular arrow head, which represents the turtle, pointing to a specific direction that you can change.Also the display is done in slow animation. Learn to change these defaults, as you may want a different behavior when you plot your graph.
Please refer to the "Turtle star" program given at the top of Section 24.1 for a short example of what can be done.
Step 2 - learn to draw proper diagrams
A proper diagram is one that has controlled appearances, such as sizes, shapes and colours. Do these exercises:
- Create a display window of specific size using the setup() function
- In the window, plot the axes of a graph which should be perpendicular to each other. You should be able to control the extent of the axes in both directions.
- Label the axes with appropriate text like "distance", "time", etc. depending on the need
- Place tick marks sensibly at regular intervals along the axes
- Label the tick marks with appropriate values
- Learn to place rectangular bars of given sizes where you want them.
Some of the exercises will require you to design simple algorithms to achieve the outcome, such as placing the tick marks at appropriate locations and label them with appropriate values. You decide what "appropriate" is.
Step 3 -Display things in real coordinates
We have not yet mentioned the coordinate system in which you do your drawing. It is likely that you have simply done the drawing so far using the native coordinate system of turtle, which is the pixel coordinates of the screen. That's because you have not specified a coordinate system.
In real life, things come in different shapes and sizes, and they all have to be drawn on a screen that has a fixed maximum size, based on the number of pixels in the x and y directions. That is the "screen coordinates" or "device coordinates". Real things do not conform to the screen coordinates, but have sizes and positions of their own. A thing 10m long can be displayed on a screen, another thing 0.1m long can be displayed on the same screen, but yet looks equally big. Clearly they are displayed at different scales. Hence you need to learn to display things at the appropriate scale so that they appear comfortable to the eyes when displayed.
You should create algorithms/functions to do the following:
- Create a display window of some specific size, as you did last week.
- Decide on the dimension of the object you wish to display, and create a function that would scale it to allow it to be displayed appropriately on the screen
- Generate an object of your choice (such as a rectangle) with its own dimensions, and display it appropriately on the screen
Step 4 - Plot the bar chart of a dataset
By now you should have gathered the tools you need to draw real bar charts. You are required to write a program to display the dataset given in the text file bulb.txt. The file contains one column of numbers, which are the number of hours a bulb burns continuously before expiring. You are required to plot a bar chart showing the distribution of these hours, by dividing the range of the measurements into 10 segments, from the low to the high, count the number of measurements in each segment and plot the number against the segments as a bar chart.
Computational thinking:
You need to separate the organization of the data from the plotting of the bar chart. The chart is only a display of the data in the way you need to visualize it. So you first need to organize the data, then display it in a bar chart. The description above suggests to you the steps.
1. Find the range of the hours.
2. Divide this range into ten segments, each segment is for 1/10 of the range.
3. Count the number of measurements in each range.
4. Plot the bar chart for the ten segments. You need to decide what the chart should contain.
5. The plotting of the bar chart itself is a major task, and you should have gathered the computational process for it from the earlier exercises.
Attachment:- pyt.zip