Skip to content

Instantly share code, notes, and snippets.

@CatChenal
Last active July 27, 2022 19:57
Show Gist options
  • Save CatChenal/2cd161705fb84293acdda87e44d3252c to your computer and use it in GitHub Desktop.
Save CatChenal/2cd161705fb84293acdda87e44d3252c to your computer and use it in GitHub Desktop.
Candle plot for two series with categorical y-axis

candle

Data points and sample figure to replicate in E. Downey's plotting

import matplotlib.pyplot as plt

sandwich_list = [
    'Lobster roll',
    'Chicken caesar',
    'Bang bang chicken',
    'Ham and cheese',
    'Tuna and cucumber',
    'Egg',
]
boston_price_list = [9.99, 7.99, 7.49, 7, 6.29, 4.99]
london_price_list = [7.5, 5, 4.4, 5, 3.75, 2.25]

fig, ax = plt.subplots(figsize=(5,6))
plt.grid()

plt.title("Selected Pret A Manger sandwiches (US\$ price)\nAug 2019\n\n")

plt.plot(london_price_list, sandwich_list, 's', color='C0', label='London')
plt.plot(boston_price_list, sandwich_list, 's', color='C3', label='Boston')
plt.hlines(sandwich_list, london_price_list, boston_price_list)

# show every other tick:
lims = ax.get_xlim()
lims = round(lims[0],0), round(lims[1],0)
ax.xaxis.set_ticks(np.arange(lims[0], lims[1]+2, 2))

ax.tick_params(axis="y", labelsize=12)
ax.tick_params(bottom=False, labelbottom=False,
               top=True, labeltop=True,
               left=True, labelleft=True
               )
for side in ['top','right','bottom']:
  ax.spines[side].set_visible(False)

plt.legend(loc=(.2,1.08), ncol=2, frameon=False, fontsize=11)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment