Skip to content

Instantly share code, notes, and snippets.

@deparkes
Last active December 6, 2016 20:20
Show Gist options
  • Save deparkes/cb23499ffa4226ac8ee6 to your computer and use it in GitHub Desktop.
Save deparkes/cb23499ffa4226ac8ee6 to your computer and use it in GitHub Desktop.
Pandas plotting of pizza price comparison
# -*- coding: utf-8 -*-
import pandas as pd
import matplotlib.pyplot as plt
import itertools
# Load data
prices = pd.read_csv("PizzaPrices.csv")
# Filter out an unwanted establishment
prices = prices[(prices['establishment_name'] != "Pizza Zone")]
# Sort data
prices = prices.sort(['establishment_type', 'establishment_name', 'Price'], ascending=[1, 1, 1])
# Set marker types to cycle through
marker1 = itertools.cycle(('s', 'o', '^', 'v', '*'))
marker2 = itertools.cycle(('s', 'o', '^', 'v', '*'))
marker3 = itertools.cycle(('s', 'o', '^', 'v', '*'))
# Group data according to establishment type
grouped = prices.groupby("establishment_name")
# Have a look at the colormaps here and decide which one you'd like:
num_plots = len(grouped)
colormap = plt.cm.gist_rainbow
# Loop through grouped data
# Change plot style depending on group
for key, grp in grouped:
if grp.establishment_type.irow(0) == "Shop":
this_marker = marker1.next()
this_colour = "red"
elif grp.establishment_type.irow(0) == "Takeaway":
this_marker = marker2.next()
this_colour = "green"
elif grp.establishment_type.irow(0) == "Restaurant":
this_marker = marker3.next()
this_colour = "blue"
plt.plot(grp['Diameter'],grp['Price'], label=key, linestyle = '-', marker=this_marker, color = this_colour, ms = 9, linewidth = 0.5)
# Plot data
plt.legend(loc=2,bbox_to_anchor=(1, 1.03))
plt.xlabel("Diameter (inch)")
plt.ylabel("Price per sq. inch (GBP/sq. inch)")
plt.xlim([5,20])
plt.grid()
# Save figure to file
save_image_as = "pizza_prices.png"
fig = plt.gcf()
fig.set_size_inches(8,6)
fig.savefig(save_image_as,dpi=600,bbox_inches='tight')
plt.show()
establishment_name establishment_type Pizza_name Diameter Area Price Price_per_sq_inch
Luigi's Takeaway 9 63.61725 4 0.06288
Luigi's Takeaway 12 113.09734 6 0.05305
Luigi's Takeaway 15 176.71459 8 0.04527
Domino's Takeaway Personal 7 38.48 3.99 0.10369
Domino's Takeaway Small 9.5 70.88 7.99 0.11273
Domino's Takeaway Medium 11.5 103.87 9.99 0.09618
Domino's Takeaway Large 13.5 143.14 11.99 0.08376
Pizza Zone Takeaway 9 63.61725 4 0.06288
Pizza Zone Takeaway 10 78.53982 5 0.06366
Pizza Zone Takeaway 12 113.09734 6 0.05305
Sainsbury's Shop Normal 14 153.93804 3.75 0.02436
Sainsbury's Shop Basics 12 113.09734 1.55 0.01371
Sainsbury's Shop Taste The Difference 12 113.09734 4 0.03537
Papa John's Takeaway S 10 78.53982 7.99 0.10173
Papa John's Takeaway M 12 113.09734 9.99 0.08833
Papa John's Takeaway L 14 153.93804 11.99 0.07789
Papa John's Takeaway XXL 16 201.06193 14.99 0.07455
Amigo's Takeaway S 8 50.26548 3 0.05968
Amigo's Takeaway M 10 78.53982 4.5 0.0573
Amigo's Takeaway L 12 113.09734 6 0.05305
Amigo's Takeaway XL 14 153.93804 7.5 0.04872
Pizza Express Restaurant 11 95.03318 7.55 0.07945
Pizza Hut Restaurant 11 95.03318 6.95 0.07313
Pizza Hut Restaurant 14 153.93804 11.95 0.07763
Dino's Takeaway 9 63.61725 4.5 0.07074
Dino's Takeaway 12 113.09734 6 0.05305
Dino's Takeaway 16 201.06193 8 0.03979
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment