Reputation: 20119
I have
fig, (((ax1, ax2), (ax3, ax4))) = plt.subplots(ncols=2, nrows=2,
sharex='col',
sharey='row',
figsize=(12, 12))
and I want to repeat one plot, say P in all axes, something like:
P.plot(ax = [ax1, ax2, ax3, ax4], facecolor = "none",
edgecolor = "black")
without having to repeat the line for each plot. Is there a way to do that?
Upvotes: 2
Views: 456
Reputation: 39042
This is one way to do this using list comprehension method without having to write four separate plot commands explicitly. I am using a DataFrame as p
to be consistent with your problem. You can try replacing df
with your p
variable.
import pandas as pd
import matplotlib.pyplot as plt
fig, (((ax1, ax2), (ax3, ax4))) = plt.subplots(ncols=2, nrows=2,
sharex='col',
sharey='row',
figsize=(8, 8))
df = pd.DataFrame({"x": [1, 2, 3, 4],
"y" : [1, 4, 9, 16]})
_ = [df.plot(x="x", y="y", ax=ax) for ax in [ax1, ax2, ax3, ax4]]
plt.show()
Upvotes: 1
Reputation: 339052
Some people prefer to use map
instead of for
in python. So I suppose if the aim is to replace some canonical loop like
for ax in [ax1, ax2, ax3, ax4]:
geodf.plot(ax=ax)
you could do
list(map(lambda ax: geodf.plot(ax=ax), [ax1, ax2, ax3, ax4]))
Upvotes: 3