How to Plot Multiple Dataframes in Subplots

How to plot multiple dataframes in subplots

You can manually create the subplots with matplotlib, and then plot the dataframes on a specific subplot using the ax keyword. For example for 4 subplots (2x2):

import matplotlib.pyplot as plt

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

df1.plot(ax=axes[0,0])
df2.plot(ax=axes[0,1])
...

Here axes is an array which holds the different subplot axes, and you can access one just by indexing axes.

If you want a shared x-axis, then you can provide sharex=True to plt.subplots.

Plotting different pandas dataframes in one figure

df.plot doesn't integrate too well in more complex plotting patterns. It's meant more as a shortcut to get a quick plot from your Dataframe while doing data exploration.

Thus, you end up creating 4 empty subplots and 4 df.plot(). I imagine that the first 3 df.plot results plots are overridden by the last one. This is probably due to the figure/axes handling of df.plot which is outside the scope of this question.

Try something like this instead.

fig, axes = plt.subplots(4, 1)

for i,name in enumerate(names):
df = pd.read_excel(myPath+name)
df['Datetime'] = pd.to_datetime(df['Datetime'])
df = df.set_index('Datetime')
axes[i].plot(df)

plt.show()

For more information on proper use of subplots, have a look at these examples: https://matplotlib.org/3.1.0/gallery/subplots_axes_and_figures/subplots_demo.html

EDIT: I had misread your code and my previous comments about specifying the keys were probably erroneous. I have updated my answer now. Hope it works for you

pandas dataframe subplot on multiple dataframe and columns showing same columns on same graph

Try with:

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

for c, ax in zip(pd1, axes):
pd1[c].plot(ax=ax, label=f'{c} pd1')
pd2[c].plot(ax=ax, label=f'{c} pd2')

ax.legend()

Output:

Sample Image

Plot separate pandas dataframe as subplots

If you have only one dimension (like 2 x 1 subplots), you can just used axes[0] and axes[1]. When you have two dimensional subplots (2 x 3 subplots for example), you indeed need slicing with two numbers.

Plotting different groups of a dataframe in different subplots

You could use seaborn.relplot:

import numpy as np
import seaborn as sns
# dummy data
df = pd.DataFrame({'Category1': np.random.choice(['A','B'], size=100),
'Category2': np.random.choice(['w','x', 'y', 'z'], size=100),
'x': np.random.random(size=100),
'y': np.random.random(size=100),
})
# plot
sns.relplot(data=df, x='x', y='y', col='Category2', col_wrap=2, hue='Category1')

Output:
seaborn relplot

How to Iterate over multiple DataFrames and plot histograms for each feature with each data set's feature in the same graph

IIUC, you could assign a new column named ID to both data frames that could be used for your legend to distinguish between your histograms. Then, you can concatenate your data frames row-wise using pd.concat. After that, you just need to define your axis and figure and iterate over all columns except of the new assigned one and plot a histogram using seaborn while distinguishing between your assigned variable. The implementation of such a distinction is straight-forward in seaborn, just use the argument hue.

Possible Code:

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

# Note: next time when asking something on SO, please provide data as code like this,
# it makes it easier for the community to replicate your problem and to help you
df1 = pd.DataFrame({
"Age": [23, 23, 24, 20],
"BsHgt_M": [1.84, 1.68, 1.6, 1.59],
"BsWgt_Kg": [113, 113.9, 86.4, 99.2],
"GOAT-MBOAT4_F_BM": [-1.623634, -1.073523, -0.270883, -0.218071],
"TCF7L2_M_BM": [0.321379, -0.957523, -0.004106, 0.568458],
"UCP2_M_BM": [0.199183, 0.549469, 1.479865, -0.398410]
})

df2 = pd.DataFrame({
"Age": [29, 30, 44, 30],
"BsHgt_M": [1.94, 1.61, 1.3, 1.19],
"BsWgt_Kg": [123, 113.9, 56.4, 91.2],
"GOAT-MBOAT4_F_BM": [-1.623676, -1.073523, -0.270883, -0.218071],
"TCF7L2_M_BM": [0.321379, -0.957523, -0.004106, 0.549469],
"UCP2_M_BM": [0.199183, 0.5499, 1.479865, -0.398410]
})

df1["ID"] = "df1"
df2["ID"] = "df2"

df = pd.concat([df1, df2]).reset_index(drop=True)
cols = df1.columns[:-1]

assert (cols == df2.columns[:-1]).all()

fig, ax = plt.subplots((len(cols)), figsize=(6, 14), sharex=False)
for i, col in enumerate(cols):
sns.histplot(data=df, x=col, hue="ID", ax=ax[i])
if i > 0: ax[i].legend(list(), frameon=False)
ax[i].set_ylabel(col)
sns.move_legend(ax[0], "upper left", bbox_to_anchor=(1, 1))
ax[-1].set_xlabel("")
plt.show()

This code plots histograms for all columns.

For two columns, it would look somewhat like this:

plot

If needed, the style and form can easily be adjusted. This is just an example of a possible solution to your problem and should only serve as a guideline.

Plotting multiple graphs on one figure generated from for loop

using subplots:

# sample data
df = pd.DataFrame(
{f"col_{i}": np.random.randint(0,100, 50) for i in range(12)}
)

fig, axs = plt.subplots(4, 3, figsize=(25,20))
all_axs = axs.ravel()
for i, c in enumerate(df.columns):
ax = df[c].plot(kind = 'barh',
width= 0.85,
stacked = True,
color = ['limegreen','gold', 'orange', 'red'],
ax=all_axs[i])
ax.legend(title = "Categories")
ax.set_xlabel("Cumulative Percent")
ax.set_ylabel("Data Stream")
ax.xaxis.set_major_formatter(mtick.PercentFormatter())
ax.invert_yaxis()

fig.tight_layout()

output:
Sample Image



Related Topics



Leave a reply



Submit