Skip to content

Instantly share code, notes, and snippets.

View BexTuychiev's full-sized avatar
🏠
Working from home

bexgboost BexTuychiev

🏠
Working from home
View GitHub Profile
fig, ax = plt.subplots(nrows=1, ncols=2)
print(ax)
print(type(ax))
___________________
[<AxesSubplot:> <AxesSubplot:>]
<class 'numpy.ndarray'>
# Unpacking method
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))
fig.tight_layout(pad=3)
ax1.plot(climate_change['date'], climate_change['co2'])
ax2.plot(climate_change['date'], climate_change['relative_temp'])
plt.show();
# Indexing method
fig, ax = plt.subplots(1, 2, figsize=(10, 5))
fig.tight_layout(pad=3)
ax[0].plot(climate_change['date'], climate_change['co2'])
ax[1].plot(climate_change['date'], climate_change['relative_temp'])
plt.show();
# Unpacking method
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(10, 5))
# .....
# Indexing method
fig, ax = plt.subplots(2, 2, figsize=(10, 5))
# ax[0, 0].plot(___)
# ax[1, 1].plot(___)
ax.shape
_____________
(2, 2)
# Additional method if you don't want to use double indexes
fig, ax = plt.subplots(3, 2)
ax.flat[0].plot(climate_change['co2']);
# Setting title, labels, etc.
fig, ax = plt.subplots()
ax.bar(medals.index, medals['Gold'])
ax.set(title='# gold medals by country', ylabel='# of medals', xlabel='Country')
ax.set_xticklabels(medals.index, rotation=90)
plt.show();
fig, ax = plt.subplots()
ax.bar(medals.index, medals['Gold'])
ax.set_xticklabels(medals.index, rotation=90)
ax.set_xlabel('Country', fontsize=15)
ax.set_title("# gold medals by country", fontsize=20)
plt.show();
# Create a figure and an axis
fig, ax = plt.subplots()
# Plot CO2 emissions with a blue line
ax.plot(climate_change['date'], climate_change['co2'], color='blue')
# Specify that we will be using a twin x axis
ax2 = ax.twinx()
ax2.plot(climate_change['date'], climate_change['relative_temp'], color='red')
# Create a figure and an axis
fig, ax = plt.subplots()
# Plot CO2 emissions with a blue line
ax.plot(climate_change['date'], climate_change['co2'], color='blue')
# Specify that we will be using a twin x axis
ax2 = ax.twinx()
ax2.plot(climate_change['date'], climate_change['relative_temp'], color='red')