Last active
May 8, 2017 10:17
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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