<jupyter_start><jupyter_text>Plotting Graphs in Python  I reccommend using matplotlib to plot graphs in python. It has useful tools and lots of documentation.  To import matplotlib plotting into your python file use the following import command:  ```pythonfrom matplotlib import pyplot as plt ```  Then to call `matplotlib.pyplot` all we need to do is call `plt`  Different Plot Types matplotlib can plot multiple different plot types from histograms to line graphs. Some useful plot types and their respective function calls are as follows:| Plot Type     | Command       ||---------------|---------------|| Line Chart    | plt.plot()    || Bar Chart     | plt.bar()     || Histogram     | plt.hist()    || Scatter       | plt.scatter() || Box + Whisker | plt.boxplot() || Pie Chart     | plt.pie()     |  Basic plotting Plotting using matplotlib generally uses one format.1. Define your data2. Plot your data3. Label your plot4. Adjust as needed<jupyter_code>from matplotlib import pyplot as plt #Import the commands!

# Then, I'll get our data. I'm going to import our data from the MIT-BIH. I'll use a segment of a sample instead of the whole thing
import wfdb # wfdb reads the data from the raw files

subj_data = wfdb.rdsamp('../mit-bih-raw/07910')#First, I'll load all of the signal data

# Then I'll extract just what we need, which is a segment from a single lead

mydata = [x[0] for x in subj_data[0]] # So, I'm going to take just the first array item (or first signal data) from the first item in what rdsamp returns which is (signal, information)
mydata = mydata[:1000] # Then, I'll take just the first 1,000 readings
print(mydata[:100]) 
   # Now I've got some data. 
plt.figure(figsize = (15, 8))
   # My first step is usually to resize the figure to be larger so That I can see the data more clearly. 
   # The format is plt.figure(figsize = (width, height))
plt.plot(mydata)<jupyter_output><empty_output><jupyter_text>As you can see by looking, we can clearly see that there are 4 heartbeats. But, we want to make this plot as clear as it can be. So, we'll add some labels and alter the axes.<jupyter_code>plt.figure(figsize = (15, 8))
   # Resize our figure again
plt.plot(mydata, label = "ECG1 Signal")
   # Plot the data, this time adding a label 
    
plt.xlabel('Sample Number') # Add a x-axis label
plt.ylabel('Signal Frequency') # Add a y-axis label

plt.title('First 1,000 Samples from Signal 1 in Subject 07910') # Add a title<jupyter_output><empty_output><jupyter_text>Sample Number isn't the clearest way to express this information, so let's change it to seconds. Given that the samples were taken at a rate of 250 samples per second we can build an array representing that.<jupyter_code>inseconds = [] # as always, initialize our array

for x in range(1000): # we took the first 1000 samples
    inseconds.append(x/250) #Then we convert from samples to seconds by dividing by 250

# Now, we can plot again and just change a few things

plt.figure(figsize = (15, 8))
   # Resize our figure again
plt.plot(inseconds, mydata, label = "ECG1 Signal")
   # Plot the data. If one array of values is given it takes the data as the y values and their location in the array as the x-values
                  # If two arrays of values are given it takes the first array as the x-values and the second array as the y-values
                  # NOTE: BOTH ARRAYS MUST BE THE SAME SIZE
    
plt.xlabel('Time (seconds)') # Change our x label to reflect our change above
plt.ylabel('Signal Frequency') # Add a y-axis label

plt.title('First 1,000 Samples from Signal 1 in Subject 07910') # Add a title<jupyter_output><empty_output><jupyter_text>Let's mess with some qualities of our new plot, like text size, color, and labels.<jupyter_code>plt.figure(figsize = (15, 8))
   # Resize our figure again
plt.plot(inseconds, mydata, label = "ECG1 Signal")
   # Plot the data. If one array of values is given it takes the data as the y values and their location in the array as the x-values
                  # If two arrays of values are given it takes the first array as the x-values and the second array as the y-values
                  # NOTE: BOTH ARRAYS MUST BE THE SAME SIZE
            
plt.legend() 
   # Since we have a Label with our plt.plot command we can create a legend with one easy command
    
plt.xlabel('Time (seconds)') # Change our x label to reflect our change above
plt.ylabel('Signal Frequency') # Add a y-axis label

plt.title('First 1,000 Samples from Signal 1 in Subject 07910') # Add a title
plt.figure(figsize = (15, 8))
   # Resize our figure again
plt.plot(inseconds, mydata, label = "ECG1 Signal")
   # Plot the data. If one array of values is given it takes the data as the y values and their location in the array as the x-values
                  # If two arrays of values are given it takes the first array as the x-values and the second array as the y-values
                  # NOTE: BOTH ARRAYS MUST BE THE SAME SIZE
            
plt.legend(fontsize = "large") 
   # we can change text size with the fontsize variable
    
plt.xlabel('Time (seconds)', fontsize = "large") # Change our x label to reflect our change above
plt.ylabel('Signal Frequency', fontsize = "large") # Add a y-axis label

plt.title('First 1,000 Samples from Signal 1 in Subject 07910', fontsize = "x-large") # Add a title
plt.figure(figsize = (15, 8))
   # Resize our figure again
plt.plot(inseconds, mydata, label = "ECG1 Signal", color = "red")
   # Plot the data. If one array of values is given it takes the data as the y values and their location in the array as the x-values
                  # If two arrays of values are given it takes the first array as the x-values and the second array as the y-values
                  # NOTE: BOTH ARRAYS MUST BE THE SAME SIZE
            
plt.legend(fontsize = "large") 
   # we can change text size with the fontsize variable
    
plt.xlabel('Time (seconds)', fontsize = "large") # Change our x label to reflect our change above
plt.ylabel('Signal Frequency', fontsize = "large") # Add a y-axis label

plt.title('First 1,000 Samples from Signal 1 in Subject 07910', fontsize = "x-large") # Add a title
plt.figure(figsize = (15, 8))
   # Resize our figure again
plt.plot(inseconds, mydata, label = "ECG1 Signal", color = "red", linestyle = "--", linewidth=2)
   # Plot the data. If one array of values is given it takes the data as the y values and their location in the array as the x-values
                  # If two arrays of values are given it takes the first array as the x-values and the second array as the y-values
                  # NOTE: BOTH ARRAYS MUST BE THE SAME SIZE
            
plt.legend(fontsize = "large") 
   # we can change text size with the fontsize variable
    
plt.xlabel('Time (seconds)', fontsize = "large") # Change our x label to reflect our change above
plt.ylabel('Signal Frequency', fontsize = "large") # Add a y-axis label

plt.title('First 1,000 Samples from Signal 1 in Subject 07910', fontsize = "x-large") # Add a title
plt.figure(figsize = (15, 8))
   # Resize our figure again
plt.plot(inseconds, mydata, label = "ECG1 Signal", color = "red", linestyle = "--", linewidth=2)
   # Plot the data. If one array of values is given it takes the data as the y values and their location in the array as the x-values
                  # If two arrays of values are given it takes the first array as the x-values and the second array as the y-values
                  # NOTE: BOTH ARRAYS MUST BE THE SAME SIZE
            
plt.annotate("R-Peak", xy = (.85, 1.25), xytext = (1, 1.22), arrowprops=dict(facecolor='black'))

plt.legend(fontsize = "large") 
   # we can change text size with the fontsize variable
    
plt.xlabel('Time (seconds)', fontsize = "large") # Change our x label to reflect our change above
plt.ylabel('Signal Frequency', fontsize = "large") # Add a y-axis label

plt.title('First 1,000 Samples from Signal 1 in Subject 07910', fontsize = "x-large") # Add a title<jupyter_output><empty_output><jupyter_text>A good tutorial on labels can be found here: https://jakevdp.github.io/PythonDataScienceHandbook/04.09-text-and-annotation.html<jupyter_code>plt.hist(mydata)
plt.hist(mydata, bins = 100)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.087689508793208, 'fraction_numerical': 0.019284414796846575, 'mean_word_length': 3.362962962962963, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '373587', 'license': 'MIT', 'n_tokens_mistral': 2608, 'n_tokens_neox': 2363, 'n_words': 1136, 'path': 'Tutorials/MatPlotLib_Tutorial.ipynb', 'repo_name': 'NinoesJo/Afib-svsm', 'size': '8245'}