1

Looking to combine two visuals into a single graphic in python much like you can combine in R. I'm running into issues with it actually combining.

Using pandas and seaborn packages I have a dataframe of some hockey stats that I want to overlay two views as a visual analysis.

Sample of the data below:

player position gp g a tp ppg pim
Nikita Kucherov RW 68 33 52 85 1.25 38
Steven Stamkos C/W 57 29 373 66 1.16 22
Alex Killorn LW 68 26 23 49 0.72 20

I wanted a stacked bar chart of goal types (pph, a, g) and then an overlay of a line chart for penalized minutes (pim)

Here is the code I've tried most recently and it just gives me two separate visuals.

lightning2022top.plot(x="player", y=["ppg", "a", "g"], kind="bar", stacked = True,figsize=(15,10))
lightning2022top.plot(x="player",y = "pim", secondary_y = True)
plt.show()

UTFG has yet to show a concise example of how to combine like I would in ggplot with R. Any help would be appreciated.

bfris
  • 5,272
  • 1
  • 20
  • 37

1 Answers1

1

Since pandas plots return ax, this can be accomplished by adding ax to the second graph.

import matplotlib.pyplot as plt
ax = df.plot(x="player", y=["ppg", "a", "g"], kind="bar", stacked=True, figsize=(8,6))
df.plot(x="player", y="pim", kind='line', ax=ax, secondary_y=True)
plt.show()

enter image description here

r-beginners
  • 31,170
  • 3
  • 14
  • 32