How to use matplotlib.subplot to plot in right place

Hi,
I’m plotting with this pieces of code:

fig = plt.figure(dpi=600)
record_df.plot(subplots=True)

here record_df is a numpy data frame with several columns. My purpose is to plot these columns with seperate plots with each set to 600 dpi. But when I run this code, there will prompt two figures: one with empty while other with several plots, which should be generated by record_df.plot(subplots=True). How can I tell python the record_df with subplots should be plotted within the first figure, i.e. fig = plt.figure(dpi=600)? thanks.

You need to first create subplots with pyplot's subplots method.

fig, axes = plt.subplots(nrows=2, ncols=1, **additional_figure_kwargs)

You can then, if needed, feed the appropriate axis to the dataframe’s plot method with the ax kwarg or specify them with the layout kwarg (with subplots=True like you’ve already done). If you’re using multiple dataframes sometimes it’s easier to use matplotlib’s system directly instead of relying on the panda’s convenience methods: matplotlib.pyplot.subplots — Matplotlib 3.5.1 documentation

Hi Kylec,
thanks for your kind reply. I read some tutorial to learn how to use “axes” with subplot. In that way I need to allocate seperate axe to each subplot. Is there a way that I can change the “focus”, so that I can directly use something like:

fig = plt.figure()
record_df.plot(subplots=True, figure=fig)

thanks.

skyworld_chen

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.