Simple Plotter

Last updated: May 12th, 20192019-05-12Project preview
In [ ]:
import matplotlib.pyplot as plt    #just loading some packages we need for plotting that aren't loaded by default in python
import numpy as np

#define a variable as the name of your data file so we can use that variable and don't have to change the name in a bunch of places for different plots

graph_filename='school_plot'   #set equal to the name you want it to save the graph as

#loading your data: first we tell it where the file is and what it's called (using the variable we defined), then 'skiprows' has it skip the first 
#row of your data since that is probably labels and it will get confused--you can change this, then 'delimiter' is what separates the two columns
#in your data--it's currently looking for a comma, but you can change this to something else (look at the format of the sample data file I uploaded)
data=np.loadtxt('Folder to upload data files/'+data_filename+'.txt', unpack=True, skiprows=1, delimiter=',')   

x_data=data[0]  #sets the x and y data as separate variables so we can work with them more easily
y_data=data[1] #this is assuming x is the first column and y is the second column

def plot_that_shit():   #we define a function that will plot our data, everything that is indented under it is part of this function
    #sets how big the figure will be in inches and then resolution in dots per inch. You can change any of these but probably easiest 
    #to just make the figsize large and just shrink it down later
    #plots the data, I put in some options by default that you can change, but there are many more you can add. You can find a list 
    #of a lot of them here, as well as the symbols for different colors and linestyles, etc  
    plt.plot(x_data, y_data, linewidth=2, color='b', linestyle='-', marker='.', markersize=10) 
    #makes a title and axes labels and sets the fontsize, but again there are many more options you can add
    plt.title("David's years in school over time", fontsize=25)  
    plt.xlabel('Age', fontsize=20)
    plt.ylabel('Years in School', fontsize=20)
    #setting the span of the x and y axes
    #I'm letting it choose it's own by default, but you can put in the [min,max] values you want if you don't like how it does it
    plt.ylim([None, None]) 
    plt.xlim([None, None]) 
    plt.yticks(fontsize='10') #size of the numeric labels on the axes
    #saves the graph as a png with the filename you set at the top in a folder, put a "#" in front of that line if you don't want it to save it"
    plt.savefig('Folder where graphs are saved/'+graph_filename+'.png') 
    #the graph is shown below and saved by default, but if you delete the "#"" at the start of the next line, it will only save it
plot_that_shit()   #runs the function that we just defined
In [ ]:
In [ ]:
Notebooks AI
Notebooks AI Profile20060