How to combine 2 graphs in one figure

The below code returns 2 seperate graphs, how can I combine them in one figure?

sns.set_style("darkgrid", {"axes.facecolor":"0.9",'':['Roboto']})

pltsize = (10,7)

yrlsum.plot(x='year', y='TAVG', kind='bar', legend=False, figsize=pltsize,
        color=(yrlsum['TAVG'] > 0).map({True:'darkorange', False:'royalblue'}))

yrlsum['MA'] = yrlsum['TAVG'].rolling(window=10).mean()

yrlsum.plot(x='year', y='MA', kind='line', legend=False, figsize=pltsize,
        color='black', lw=1)

By Manos Mourtzanos via Discussions on at 24Apr2022 17:45:

The below code returns 2 seperate graphs, how can I combine them in one

Can you show us the complete programme? Which libraries is it using? How
are “sns” and “yrlsum” and “plt” defined?

Cameron Simpson

By standard convention, plt is matplotlib.pyplot and sns is seaborn, and from the context, yrlsum is a pandas.DataFrame (though of course, all of his should have been shown). The Seaborn code is apparently unrelated, since df.plot() just uses matplotlib, unless those specific calls pass through to Matplotlib or the user changed the plotting.backend option of pandas (though it still is not really relevant).

See the documentation for DataFrame.plot(). In summary, instead of letting pandas create a figure for you, create one with two subplots (axes) and use the ax parameter to pass one to each plot call. Here’s a simple self-contained example that will stack both plots on top of each other:

import matplotlib.pyplot as plt
import pandas as pd

# Create some example data similar to yours
df = pd.DataFrame({"year": [1, 2, 3, 4, 5], "TAVG": [20, 25, 23, 27, 30]})
df["MA"] = df["TAVG"].rolling(window=2).mean()

# Create a common figure for both with 2 columns and 1 row of subplots
figure, axes = plt.subplots(2, 1, figsize=(10, 7))

# Create each subplot on the specified axes
df.plot(x="year", y="TAVG", kind="line", ax=axes[0])
df.plot(x="year", y="MA", kind="bar", ax=axes[1])
1 Like

I want them to be both in the same place like this:

Sorry, but the original question was not clear on this point; it merely asked for both plots to be on the same figure, which the above approach accomplished. Next time, make sure to be detailed and specific with regard to your requirements, and sharing an example up front would have been particularly helpful.

In any case, if that’s what you want, its even simpler. You just call plt.subplots() with no arguments (or just figsize), i.e.

figure, axes = plt.subplots(figsize=(10, 7))

and pass the same single returned axes object to ax in both df.plot() functions. This plots both plots on the same axes, as you’re asking for (you do have to be a bit careful, since those are two totally different plot types and don’t always mix well on the same axis).

Making these simple changes in the above example, and adding in your colors and generating slightly more realistic example data, we get a result very similar to what you’re looking for:


Hi could you show me the complete code? I still get it wrong. Thanks in advance