Skip to content

Instantly share code, notes, and snippets.

@chankeypathak
Last active May 8, 2017 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chankeypathak/a9227df17b13ae76ba64fb150e90af3b to your computer and use it in GitHub Desktop.
Save chankeypathak/a9227df17b13ae76ba64fb150e90af3b to your computer and use it in GitHub Desktop.
matplotlib basic example: Importing libraries - Creating data sets - Creating data frames - Reading from CSV - Exporting to CSV - Finding maximums - Plotting data
import pandas as pd #this is how I usually import pandas
import matplotlib.pyplot as plt
names = ['Bob','Jessica','Mary','John','Mel']
births = [968, 155, 77, 578, 973]
BabyDataSet = list(zip(names,births))
df = pd.DataFrame(data = BabyDataSet, columns=['Names', 'Births'])
# Create graph
df['Births'].plot()
# Maximum value in the data set
MaxValue = df['Births'].max()
# Name associated with the maximum value
MaxName = df['Names'][df['Births'] == df['Births'].max()].values
# Text to display on graph
Text = str(MaxValue) + " - " + MaxName
# Add text to graph
plt.annotate(Text, xy=(1, MaxValue), xytext=(8, 0),
xycoords=('axes fraction', 'data'), textcoords='offset points')
print("The most popular name")
df[df['Births'] == df['Births'].max()]
#Sorted.head(1) can also be used
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment