Viewing a single comment thread. View all comments

Tambury OP t1_itnx41u wrote

Here you go! Apologies for the uncommented spaghetti code.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

pizzasplit=pd.read_csv(r'D:\Downloads\pizza time splits - Sheet1.csv')

fig, ax = plt.subplots(figsize=(8, 6),dpi=150)

xlim=45.5
ylim=1005

pizzasplit['Elapsed time']=pizzasplit['Elapsed time'].map(lambda x: pd.to_timedelta(x).seconds/60)

plt.scatter(pizzasplit['Elapsed time'],pizzasplit.Millimetres,color='b')
plt.plot(pizzasplit['Elapsed time'],pizzasplit.Millimetres,color='b')
ax.axhline(1000,color='r',xmax=45/xlim)
ax.axhline(604,color='b',xmin=36.91/xlim,xmax=45/xlim)
plt.text(22.5, 1000, '1 metre of pizza', fontsize=12, va='center', ha='center', backgroundcolor='w')
plt.text(37, 620, 'Defeat', fontsize=12, va='baseline', ha='left')

ax.axvline(30,color='g',ymax=1000/ylim)
ax.axvline(45,color='g',ymax=1000/ylim)
plt.text(29.5, 30, 'Time limit for free pizza + $100', fontsize=10, va='bottom', ha='center', rotation=90)
plt.text(44.5, 30, 'Time limit for commemorative t-shirt', fontsize=10, va='bottom', ha='center', rotation=90)

x=np.arange(0, 31, 1)
y=1000*x/30
plt.plot(x,y,color='g',linestyle='dashed',alpha=0.4)

x=np.arange(0, 46, 1)
y=1000*x/45

plt.plot(x,y,color='g',linestyle='dashed',alpha=0.4)

plt.xticks(np.arange(0, 46, step=5))
plt.yticks(np.arange(0, 1001, step=250))
plt.xlim(0,xlim)
plt.ylim(0,ylim)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)

ax.grid(linestyle="--", linewidth=0.5, color='.25', alpha=0.3, zorder=-10)

plt.xlabel('Elapsed time (minutes)')
plt.ylabel('Pizza consumed (mm)')

def make_patch_spines_invisible(ax):
    ax.set_frame_on(True)
    ax.patch.set_visible(False)
    for sp in ax.spines.values():
        sp.set_visible(False)

fig.subplots_adjust(right=0.75)

par1 = ax.twinx()
par2 = ax.twinx()

# Offset the right spine of par2.  The ticks and label have already been
# placed on the right by twinx above.
par1.spines["right"].set_position(("axes", 1.02))
par2.spines["right"].set_position(("axes", 1.15))
# Having been created by twinx, par2 has its frame off, so the line of its
# detached spine is invisible.  First, activate the frame but make the patch
# and spines invisible.
make_patch_spines_invisible(par2)
# Second, show the right spine.
par2.spines["right"].set_visible(True)
par1.spines['top'].set_visible(False)

par1.set_ylim(0, (ylim/1000)*max(pizzasplit.Slices))
par2.set_ylim(0, (ylim/1000)*max(pizzasplit.kJ))

par1.set_yticks(np.arange(0, 25, step=4))

par1.set_ylabel("Slices")
par2.set_ylabel("Energy (kJ)")

tkw = dict(size=4, width=1.5)
ax.tick_params(axis='y', **tkw)
par1.tick_params(axis='y', **tkw)
par2.tick_params(axis='y', **tkw)
ax.tick_params(axis='x', **tkw)

plt.tight_layout()

Data input CSV file below

kJ,Millimetres,Slices,Elapsed time
0,0,0,00:00:00
728.5,41.66666667,1,00:01:04
1457,83.33333333,2,00:02:08
2185.5,125,3,00:02:50
2914,166.6666667,4,00:03:38
3642.5,208.3333333,5,00:04:37
4371,250,6,00:05:15
5099.5,291.6666667,7,00:06:04
5828,333.3333333,8,00:07:03
6556.5,375,9,00:08:28
7285,416.6666667,10,00:10:08
8013.5,458.3333333,11,00:12:28
8742,500,12,00:15:41
9470.5,541.6666667,13,00:21:30
10199,583.3333333,14,00:28:52
10563.25,604.1666667,14.5,00:36:55
11656,666.6666667,16,
12384.5,708.3333333,17,
13113,750,18,
13841.5,791.6666667,19,
14570,833.3333333,20,
15298.5,875,21,
16027,916.6666667,22,
16755.5,958.3333333,23,
17484,1000,24,
2