Skip to main content
Chemistry LibreTexts

2.13: Charts and Graphs with Python

  • Page ID
    206280
  • Hypothes.is tag: s20iostpy13ualr
    Download Assignment: S20py13

    Learning Objectives

    Students will be able to:

    Content

    • Generate bar graphs
    • Generate pie charts
    • Generate line graphs
    • Save your data as images
    • Process:

    • Import matplotlib on your Raspberry Pi
    • Write code that that converts data in lists to charts and graphs

    Prior Knowledge

    • Python concepts from previous activities

    Further Reading

     

    Model 1: matplotlib

    matplotlib is a python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. matplotlib can be used in python scripts, the python and ipython shell web application servers, and six graphical user interface toolkits.

    matplotlib

    matplotlibtries to make easy things easy and hard things possible. You can generate plots, histograms, power spectra, bar charts, errorcharts, scatterplots, etc, with just a few lines of code. For more information go to http://matplotlib.org/1.3.0/index.html


    Task 1: For our windows PCs, we installed Python using Anaconda. It includes the matplotlib library in the install. For safe measure, update your anaconda by opening the anaconda prompt and typing conda update anaconda. Go back to Internet of Things Activity 1 (Preparing your PC for Class for a refresher if you need).


    Task 2: For our Raspberry Pi computers, we need install the library using apt-get. Open up a terminal window on your Raspberry Pi and complete the following commands.

    pi@yourhostname:~ $ sudo apt-get update
    pi@yourhostname:~ $ sudo apt-get upgrade
    pi@yourhostname:~ $ sudo apt-get install libav-tools
    pi@yourhostname:~ $ sudo apt-get -y install Python3-matplotlib
    

    To test if matplotlib installed correctly, open up a terminal window and type python3. When you get the python prompt (shown as >>>), type in import matplotlib. If it installed correctly, you will be given another prompt (>>>) if not, you will be given an error.

    Successful test for Python 3 Failed test for Python 3

    pi@yourPiname -$ python3

    Python 3.4.2(default, Oct 19 2014, 13:13:11)

    [GCC 4.9.1] on linux

    Type “help”, “copyright”, “credits” of “license” for more information.

    >>> import matplotlib

    >>>

    Note there may be a short pause before getting the next prompt. After confirming this is working, type and you will get your prompt back

    >>> quit()

    pi@yourPiname -$

    pi@yourPiname -$ python3

    Python 3.4.2(default, Oct 19 2014, 13:13:11)

    [GCC 4.9.1] on linux

    Type “help”, “copyright”, “credits” of “license” for more information.

    >>> import matplotlib

    Traceback (most recent call last):
     File “<stdin>”, line 1, in <module>
    ImportError: No module named matplotlib
    >>>

    At the prompt you can type quit() and ask your instructor for help.
    >>> quit()

    Note

    You can do this activity on either your PC or your Raspberry Pi.

    Model 1: Bar Graphs

     

    Python Program 13.1 Output
    import matplotlib.pyplot as plt
    
    myGraphValues = [1.0446, 0.8765, 0.7739, 0.684, 0.867]
    myGraphWidths = [.5, .5, .5, .5, .5]
    myGraphColors = ['r', '#FF6600', 'b', 'DarkViolet']
    myGraphLabels = ['acetone', 'benzene', 'cyclohexane', 'heptane','toluene']
    
    x = range(0,len(myGraphLabels))
    plt.bar(x, myGraphValues, align = 'center', width=myGraphWidths,\
            color=myGraphColors)
    plt.title('Common Organic Solvent Density')
    plt.ylabel('g/mL')
    plt.xlabel('Solvent')
    plt.xticks(x, myGraphLabels)
    plt.show()
    
    figure 2: Bar graph

     

    Note

    In the first line of code, matplotlib.pyplot is imported as plt. This statement indicates that we are using the pyplot package of matplotlib, and using a shortened alias for that library. Subsequently we are using functions within the pyplot package.

    Critical Thinking Questions:

    1.  What necessary data for the bar graph is being stored as lists?
       
    2.  How does matplotlib know how many bars to draw on the graph?
       
    3.  What is the function for indicating you want to generate a bar graph.
       
    4.  What happens if you comment out the plt xticks line?

    5.  Graph colors can be assigned as 

         Using named colors, redraw the graph so that all the items are different shades of green.

    6. The default value for graph width is 0.8.

    • Change the values for myGraphWidths = [.8, .8, .8, .8. .8]. How does this alter the graph?
    • What happens if you set myGraphWidths = [.2, .8, .5, 1, 1.2]?
    1. Each of the lists has exactly 5 elements.  What happens if you add an element to one list, but not the others?
       
    2. In this activity, you will create a pie chart.  Type in this code and run it.  It will take a couple of seconds for Python to make the plot and display it, so be patient.
    Python Program 13.2
    import matplotlib.pyplot as plt
    
    myGraphValues = [20.84, 27.54, 7.73, 36.28, 7.61]
    myGraphColors = ['r', '#FF6600', 'y', 'g', 'b']
    myGraphLabels = ['$^{70}$Ge', '$^{72}$Ge', '$^{73}$Ge', '$^{74}$Ge', '$^{76}$Ge']
    explode = (0, 0, 0, .1, 0)
    plt.pie(myGraphValues, colors= myGraphColors, labels = myGraphLabels, \
            explode=explode, autopct='%1.1f%%', counterclock=False, \
            shadow = True)
    plt.title('Natural Abundance of Germanium Isotopes')
    plt.show()
    

     

    1.  A new variable called a tuple has been included named explode. Alter this line of code to determine what it does to the graph. Explain your results.
    2.  myGraphLabels requires the use of superscripts in identifying the isotopes of germanium. What is the syntax for making a superscript
    3.  The syntax for making a subscript is similar to a superscript except it uses a _ symbol. Change the title so that the word “Abundance” is all subscripted. 
    4.  Adjust each of the following arguments in the plt.pi() function. Briefly indicate what each argument affects:
      • autopct
      • counterclock
      • shadow

    Note

    In creation of the pie chart, python will calculate the percentages for you. You do not need to have all the values equal to 100 if you have raw data

    1.  In this activity, you will create a line graph.  Type in this code and run it.  It will take a couple of seconds for Python to make the plot and display it, so be patient.
    Python Program 13.3
    import matplotlib.pyplot as plt
    
    myNO2Values = [5, 40, 16, 25, 9, 10]
    plt.plot(range(1,7),myNO2Values)
    plt.title('Average Concentration over 6 days')
    plt.ylabel('Concentration(ppbv)')
    plt.xlabel('Day of experiment')
    
    plt.show()

     

    1.  What is the function for creating a line graph?
       
    2. Change line 4 in your code to read:
    plt.plot(range(len(myGraphValues)), myGraphValues)
    • What is the advantage of this line of code over the previous example?
       
    • What is the disadvantage of this line of code over the previous example?
    1.  Add the following lines before the plt.show() function
    ax = plt.axes()
    ax.set_xlim([1,6])
    ax.set_ylim([-5,50])
    • What effect did the second line have?
       
    • What effect did the third line have
    1. After the lines you just added but before the line plt.show() add the following lines.
    ax.set_xticks([1,2,3,4,5,6])
    ax.set_yticks([10,20,30,40,50])
    • What effect did the first line have?
       
    • What effect did the second line have?
       
    • What happens if you add the line 
    ax.grid()

     

    1.  Change your code to the code below.  This will make our graph prettier for the moment by removing the wacky scales now that you know how to work with them and also add a second data line to the graph!
    Python Program 13.4
    import matplotlib.pyplot as plt
    
    myNO2Values = [5, 40, 16, 25, 9, 10]
    myO2GraphValues = [10, 6, 22, 9, 15, 4]
    plt.plot(range(1,7), myNO2Values)
    plt.plot(range(1,7), myO2GraphValues)
    plt.title('Average Concentration over 6 days')
    plt.ylabel('Concentration(ppbv)')
    plt.xlabel('Day of experiment')
    ax = plt.axes()
    ax.set_xlim([1,6])
    ax.set_ylim([-5,50])
    ax.set_xticks([1,2,3,4,5,6])
    ax.set_yticks([10,20,30,40,50])
    ax.grid()
    plt.show()

     

    1. How can you add a third data line to the graph?  And a fourth and so on?
       
    2. Change this line
    plt.plot(range(1,7), myNO2Values)

    to

    plt.plot(range(1,7), myNO2Values, '-oy')

      How does that change the appearance of the graph?

    Note

    The last argument in the plt.plot line can be altered three different ways by changing the value of the '-or'shown above. They go in the order marker, line, color.  You can omit any of them.

     

    You have tons of options to replace the o with for the marker.  Each one gives you a different type of marker.  There are too many to list, but my favorites are

    • Small letter o: a circle
    • Small letter s: a square
    • An asterisk: a star
    • Capital letter D: a diamond
    • Small letter d: a thin diamond

     

    You have five options for the minus sign.  Each one gives you a different type of line:

    • Nothing: no line
    • A minus sign: solid line
    • Two minus signs): dashed line
    • Minus sign period: dash-dot line
    • Colon: dotted lin

      You can change the line color by adding a letter corresponding to a Python color.  Try it!
    1. You can add a legend to your graph by adding the command:
    plt.legend(['NO$_2$','O$_2$'], loc=2)
    1. Where does it place the legend?
       
    2. The loc argument can have values from 0 to 10.  Try some of these values.

    Note: Saving your graphs for use elsewhere

    It's possible (and even likely) that you might use Python to analyze data in a course and want to produce some nice graphs and turn them in.  You can save the graphs you generate with matplotlib and then paste them into a Microsoft Word (or other) document.  To save the graphs, you can click the disk icon circled in red below on the raspberry pi, or green on your windowsPC.

    graphs

    If you resize the window before clicking the save button, the resized image will be saved.  This is really useful if the window that came up wasn’t quite big enough to show your entire graph.  Alternatively, you can use a line of code like this.

    plt.savfig('MySampleImage.png', format='png')

    Supported file formats are png, ps, pdf, eps and svg.

    There is much more that you can do then we have to cover and this link provides more documentation,https://matplotlib.org/api/pyplot_api.html .