简体   繁体   中英

How to make my DataFrame.plot subplots posted in line?

I'm trying to understand how pandas.DataFrame.plot works, but stuck on placing several subplots in a line. I feel pretty confused, so probably my question will sound weird. But I would be grateful for any help.

recent_grads.plot(x = "Women", y = "Median", kind = "scatter", subplots = True, figsize=(6, 6), layout = (2,2), sharex = False)
recent_grads.plot(x = "Men", y = "Median", kind = "scatter", subplots = True, figsize=(6, 6), layout = (2,2), sharex = False)

I'm getting my subplots on under another, but I want them in line.

u can use plt.subplots from matplotlib.pyplot

import matplotlib.pyplot as plt
fig, ax = plt.subplots(nrows=1, ncols=2)
fig.set_size_inches(6, 6)
plt.subplots_adjust(wspace=0.2)
recent_grads.plot(x = "Women", y = "Median", kind = "scatter", ax=ax[0], sharex = False)
recent_grads.plot(x = "Men", y = "Median", kind = "scatter", ax=ax[1], sharex = False)

You just need to modify layout :

recent_grads.plot(x = "Women", y = "Median", kind = "scatter", subplots = True, figsize=(6, 6), layout = (1, 1), sharex = False)
recent_grads.plot(x = "Men", y = "Median", kind = "scatter", subplots = True, figsize=(6, 6), layout = (1, 2), sharex = False)

Another way is to create Axes objects and specify them explicitly:

from matplotlib import pyplot as plt

fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(6, 6))

ax1, ax2 = axes

recent_grads.plot(x = "Women", y = "Median", kind = "scatter", ax=ax1)
recent_grads.plot(x = "Men", y = "Median", kind = "scatter", ax=ax2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM