Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 1571
Kernel: Python 2

Plotting in Python

Matplotlib is a Python 2D plotting library which produces publication quality figures.

You can generate plots, histograms, power spectra, bar charts, errorcharts, scatterplots, etc, with just a few lines of code.

Check the link for more details: http://matplotlib.org/


Use the following line to allow the plots to be displayed as part of the jupyter notebook:

%matplotlib inline
# The following line to allow the plots to be displayed as part of the jupyter notebook %matplotlib inline # Import matplotlib's function pyplot to make simple plots import matplotlib.pyplot as plt # To avoid using big names rename the module as plt plt.plot([1,2,3,4],[2,4,6,12]) plt.show()

Plotting

Add a second line to the plot. Try to use different line colors, markers and linestyles.

Check the link for plotting: http://matplotlib.org/users/pyplot_tutorial.html

# Import matplotlib's function pyplot to make simple plots import matplotlib.pyplot as plt # Plot a line plt.plot([1,2,3,4],[2,4,6,12],label='Series1') # Add Label for the X axis plt.xlabel('X') # Add Label for the Y axis plt.ylabel('Y') # Add Title to the plot plt.title('Test Plot') plt.show()

Line Plots: Exercise

# Import matplotlib's function pyplot to make simple plots # Import math module to access sin and cosine functions import matplotlib.pyplot as plt import math x=[] # Empty list to store x axis values cos_y=[] # Empty list to store cosine values sin_y=[] # Empty list to store sine values # Create 60 data points for i in range(60): x += [0.1*i] cos_y += [math.cos(0.1*i)] sin_y += [math.sin(0.1*i)] # Plot x vs cos_y and x vs sin_y

Histograms

To make histograms, let us first create some data which is normally distributed.

To create a histogram use hist() function of matplotlib.pyplot

import matplotlib.pyplot as plt import random #Create an empty list to store data data=[] #Generate 500 data values for i in range(500): data += [random.normalvariate(10, 3)] #Plot a histogram plt.hist(data) #Set X and Y labels and plot title plt.xlabel('Values') plt.ylabel('Frequency') plt.title('Normal Distribution') plt.show()

Plotting: Exercise

  • Exercise 1

    • Create 2 random DNA sequences (random_seq1 and random_seq2) of length 500.

    • Use your own dna_tools module to count nucleotide usage (A,T,G, and C) in seq_r1 and seq_r2.

    • Make a line plot to display the nucleotide usage.

    • Use different markers for different sequences.

  • Exercise 2:

    • Generate 100 random DNA sequences of length 500.

    • Plot a histogram for 'A' nucleotide usage in the 100 random DNA sequences.

    • Add histograms of other nucleotide usage in the same histogram.

# Import module to help us generate random numbers/entries # Import counting function you created earlier # Use for loop to create two random sequences, each of length = 500 # Use your the function that you imported to get A,T,G, and C composition of the random sequences # Plot the A,T,G, and C compositions of the two random sequences in a single plot # Use different markers, labels etc to distinguish between two lines
# Import module to help us generate random numbers/entries # Import counting function you created earlier # Make a list to store 100 sequences, each of length 500 # Use for loop to create 100 random sequences, each of length = 500 # Use for loop to count bases in each sequence # Plot the results in the form of a histogram