Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When I label axes in a subplot with shared axes, not all labels show after plotting with Pandas

When creating a two-panel subplot with shared axis, I can specify the label for each axes...

import matplotlib.pyplot as plt
fig, axes = plt.subplots(1, 2, sharey=True, sharex=True)
axes[0].set_ylabel('ylabel-1')
axes[1].set_ylabel('ylabel-2')
plt.tight_layout()

enter image description here

However, when I use Pandas to plot on those axes, the shared label goes missing

import matplotlib.pyplot as plt
import pandas as pd
fig, axes = plt.subplots(1, 2, sharey=True, sharex=True)
pd.DataFrame([1,2,3]).plot(ax=axes[0])
pd.DataFrame([3,2,1]).plot(ax=axes[1])
axes[0].set_ylabel('ylabel-1')
axes[1].set_ylabel('ylabel-2')
plt.tight_layout()

enter image description here

How can I get that label to show?

like image 559
blaylockbk Avatar asked Oct 19 '25 12:10

blaylockbk


1 Answers

As described here, the label exists, but it is not visible.

Add this line to get the label to be visible.

axes[1].yaxis.get_label().set_visible(True)

For example:

fig, axes = plt.subplots(1, 2, sharey=True, sharex=True)
pd.DataFrame([1,2,3]).plot(ax=axes[0])
pd.DataFrame([3,2,1]).plot(ax=axes[1])
axes[0].set_ylabel('xlabel')
axes[1].set_ylabel('ylabel')

axes[1].yaxis.get_label().set_visible(True)
plt.tight_layout()

enter image description here

like image 128
blaylockbk Avatar answered Oct 21 '25 02:10

blaylockbk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!