DOC PREVIEW
TAMU CSCE 110 - Continuation on Properties of Graphs
Type Lecture Note
Pages 4

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CSCE 110 1nd EditionLecture 16Outline of Last Lecture:I. List ComprehensionA. BasicsB. Making programs shorterOutline of Current Lecture:I. Continuation on Properties of GraphsA. Formatting the axesB. Changing the type of graphCurrent Lecture:I. Continuation on Properties of GraphsA. Formatting the AxesWe will look at a pretty detailed program which, in a nutshell, graphs the winning number of hotdogs eaten for the annual hotdog-eating contest, spanning from 1980 to the present. You should be familiar with the bulk of the program by now; we will look at small things we can change on the graph. Comments will be written in italics underneath certain lines.1 import matplotlib.pyplot as plot2 def plot_results(x_axis, y_axis):3 fig = plot.figure(figsize=(20,5))This line determines the size of the graph produced later on. The first integer in parentheses dictates the width, while the following integer dictates the height.4 plot.plot(x_axis, y_axis, label='Curve label, can be anything', marker='o')5 plot.xticks(x_axis, x_axis)If you want to change the x-axis to show every single value given in the x-axis list that is being graphed, in the parentheses you will put the list containing the x-values being graphed, and it will be followed by the list of x-axis values that you want shown instead, or, in our case, we just rewrite the list name, meaning we want every single x-value written on the x-axis. 6 plot.autoscale(enable=True, axis='y', tight=True)This line autoscales the size of the y-axis based on the size of the x-axis.7 plot.xlim(x_axis[0] -1, x_axis[-1] + 1)This will change the range of the x-axis. The first value in the parentheses, (x_axis[0] -1), signifies that the x-axis will leave some room before the first x-value/point plotted, as if there were 1 additional value needed to be plotted to the left. This is the starting value of the range. The second value, (x_axis[-1]+1), signifies the ending value for the range to be covered by the x-These notes represent a detailed interpretation of the professor’s lecture. GradeBuddy is best used as a supplement to your own notes, not as a substitute.axis of the graph. This value is defined as being 1 more than the last value given in the list of x-values for the x-axis, leaving some room to the right of the very last point on the curve. 8 plot.ylim(0,75)Line 8 is the exact same concept as line 7, only this line is for the range of the y-axis. 9 fig.autofmt_xdate()With dates on the x-axis, it is sometimes desirable to use this "auto-format" code to slightly rotate the x-values, giving them a good amount of room on the "ticks" and making it easier to read. Unless you want to manually enter the exact degree of rotation, this line can be memorized and typed in this exact same way for any graph using dates on the x-axis. 10 plot.xlabel('Year/x-axis title')11 plot.ylabel('number of hotdogs eaten/y-axis title')12 plot.title('Nathan\'s Hotdog Eating Contest/graph title')13 plot.grid()14 plot.legend(loc='upper left')15 plot.show() 16 def main():17 filename = raw_input('Enter filename: ') 18 open_file = open(filename, 'r')19 data = open_file.readlines() 20 x_axis = []21 y_axis = []22 for i in range(1,len(data)):23 data[i] = data[i].split(',') 24 x_axis += [int(data[i][0])]25 y_axis += [float(data[i][2])]26 plot_results(x_axis, y_axis)27 main ()An alternative way of writing line 5:5 new_x = ["'"+str(i)[2:] for i in x_axis]6 plot.xticks(x_axis, new_x)This is the same concept already used. In this case, we have the years written as'79, '80, etc., written in a new list, which we later call in the xticks built-in function. B. Changing the type of graphSo far we have only worked with line plots. We can change the type of graph itself. For instance, we can write the same program as before, showing the results of the hotdog-eating contest, butthis time we will show it the form of a bar graph. 1 import matplotlib.pyplot as plot2 def bar_plot(x_axis, y_axis):It may help to change the name of your user-defined function, for consistency.3 fig = plot.figure(figsize=(20,5))4 x_label_pos = range(len(x_axis))This gets us the number of bars we want in our graph (one for each year).5 plot.bar(x_label_pos, y_axis, label='Curve label, can be anything', width=0.2)We write "plot.bar" instead of "plot.plot" if we want a bar graph. Instead of having the option for a marker for each point that would be on a line graph, we have width options. This dictates the width of each bar on the graph. The wider we want the bar, the higher the number we enter.6 x_label = [i + 0.6 for i in x_label_pos]This changes the position of the bars on the graph. Because we already have set labels for the x-axis ticks and the set number of bars to be graphed, we can change the position of the bars by slightly changing the x-values. This will put the bars in the middle of two gridlines, which helps visibility. This is completely optional. In fact, most of what we've covered in this set of notes is just for improving the aesthetics of the graph.7 plot.xticks(x_label, x_axis)Remember, this allows us to show every single year/x-value on the graph/x-axis.8 plot.xlim(-1,35)Remember, this isn't actually the range of the values listed, but the range of the amount of space allotted on the x-axis. In other words, as there are 34 years plotted and we want some space on each end of the curve, we start the range as one less than 0 and end it one more than the number of years actually graphed. 9 plot.ylim(0,75)10 fig.autofmt_xdate() 11 plot.xlabel('Year/x-axis')12 plot.ylabel('number of hotdogs eaten/y-axis')13 plot.title('Nathan\'s Hotdog Eating Contest/graph title')14 plot.grid()15 plot.legend(loc='upper left')16 plot.show() 17 def main():18 open_file = open('nathans-hotdog-data.csv', 'r')19 data = open_file.readlines()20 x_axis = []21 y_axis = []22 for i in range(1,len(data)):23 data[i] = data[i].split(',') 24 x_axis += [int(data[i][0])]25 y_axis += [float(data[i][2])]26 bar_plot(x_axis, y_axis)27 main


View Full Document

TAMU CSCE 110 - Continuation on Properties of Graphs

Type: Lecture Note
Pages: 4
Documents in this Course
06-IO

06-IO

29 pages

21-OOP

21-OOP

8 pages

key

key

6 pages

21-OOP

21-OOP

8 pages

Load more
Download Continuation on Properties of Graphs
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Continuation on Properties of Graphs and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Continuation on Properties of Graphs 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?