Retrieve and display a data set | Compass Mentis – Coen de Groot
Summary
Python is a simple but powerful language, and comes with a wealth of libraries. See the installation instructions 1. import pandas, matplotlib Grab the libraries we need to load, clean up and display the data. The recommended approach (PEP 8) is to have two import statements on separate lines. This gives us a pandas DataFrame, a two dimensional datastructure similar to a page in a spreadsheet. This gives us the 10 cereals with the highest calories per serving 5. ax = data.plot(kind=’barh’) Plot the data as a horizontal bar chartax.set_xlabel(‘Calories per serving’) 6. ax.set_ylabel(‘Cereal’) 7. ax.set_title(‘Top 10 cereals by calories’) 8. ax.set_xlabel(‘Area in millions square kilometers’) Set the label for the x and y axes and the title 9. matplotlib.pyplot.subplots_adjust(left=0.45) Set the left margin (from the left of the image to the left of the chart area) to 45% to give enough space for the cereal names.