-1

I'm trying to generate a plot in seaborn using a for loop to plot the contents of each dataframe column on its own row.

The number of columns that need plotting can vary between 1 and 30. However, the loop creates multiple individual plots, each with their own x-axis, which are not aligned and with a lot of wasted space between the plots. I'd like to have all the plots together with a shared x-axis without any vertical spacing between each plot that I can then save as a single image.

The code I have been using so far is below.

comp_relflux = measurements.filter(like='rel_flux_C', axis=1) *# Extracts relevant columns from larger dataframe comp_relflux=comp_relflux.reindex(comp_relflux.mean().sort_values().index, axis=1) # Sorts into order based on column mean.

plt.rcParams["figure.figsize"] = [12.00, 1.00]
for column in comp_relflux.columns:
    plt.figure()
    sns.scatterplot((bjd)%1, comp_relflux[column], color='b', marker='.')

This is a screenshot of the resultant plots.

I have also tried using FacetGrid, but this just seems to plot the last column's data.

p = sns.FacetGrid(comp_relflux, height=2, aspect=6, despine=False)
p.map(sns.scatterplot, x=(bjd)%1, y=comp_relflux[column])
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
msalis
  • 11
  • 1
    Use .melt on the dataframe, and then sns.relplot – Trenton McKinney Jul 10 '22 at 17:20
  • 1
    This question is not reproducible without **data**. This question needs a [SSCCE](http://sscce.org/). Please see [How to provide a reproducible dataframe](https://stackoverflow.com/q/52413246/7758804), then **[edit] your question**, and paste the clipboard into a code block. Always provide a [mre] **with code, data, errors, current output, and expected output, as [formatted text](https://stackoverflow.com/help/formatting)**. If relevant, plot images are okay. If you don't include an mre, it is likely the question will be downvoted, closed, and deleted. – Trenton McKinney Jul 11 '22 at 01:10

1 Answers1

1

To combine the x-axis labels and have just one instead of having it for each row, you can use sharex. Also, using plt.subplot() to the number of columns you have, you would also be able to have just one figure with all the subplots within it. As there is no data available, I used random numbers below to demonstrate the same. There are 4 columns of data in my df, but have kept as much of your code and naming convention as is. Hope this is what you are looking for...

comp_relflux = pd.DataFrame(np.random.rand(100, 4)) #Random  data - 4 columns
bjd=np.linspace(0,1,100) # Series of 100 points - 0 to 1
rows=len(comp_relflux.columns) # Use this to get column length = subplot length
fig, ax = plt.subplots(rows, 1, sharex=True, figsize=(12,6)) # The subplots... sharex is assigned here and I move the size in here from your rcParam as well

for i, column in enumerate(comp_relflux.columns):
    sns.scatterplot((bjd)%1, comp_relflux[column], color='b',marker='.', ax=ax[i])

1 output plot with 4 subplots

enter image description here

Redox
  • 9,321
  • 5
  • 9
  • 26