'Matplotlib time series graph with 3 Y-Axes on right side of graph
I am using matplotlib to graph 3 time series. I want all 3 y-axes to be plotted on the right side of the graph. However I am unable to get one of the y-axis values to plot on the right side, only it's axis label.
Here is my code:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import datetime as dt
import matplotlib.dates as mdates
from matplotlib.ticker import ScalarFormatter, FormatStrFormatter
from matplotlib import ticker
# Set date range for graph
df_ratios=df_ratios.loc['2017-06':'2021-12']
#-------------------------------------------------------------------
#For all Matplotlib plots, we start by creating a figure and an axes.
#-------------------------------------------------------------------
# subplot for plotting figure
fig, ax = plt.subplots()
#Similar to fig=plt.figure(figsize=(12,6))
fig.set_figwidth(12)
fig.set_figheight(6)
# Graph title
#############
fig.suptitle('Inventory-to-Sales Ratios by Industry',fontsize=20)
# LABELS: Set x label which is common / set left y-axis label / set labelcolor and labelsize to the left Y-axis
###############################################################################################################
ax.set_xlabel('Monthly: Jun 2017 - Dec. 2021')
ax.set_ylabel('Manufacturers I/S Ratio', color='red',size='x-large')
ax.tick_params(axis='y', labelcolor='red', labelsize='large')
ax.spines["right"].set_visible(True)
# Left Y-Axis: Mfg IS ratio on left Y-axis
###########################################
ax.plot(df_ratios.index, df_ratios['mfg_is_ratio'], color='red',linewidth=5.0)
ax.set_ylim(1.25,1.8)
ax.yaxis.set_major_locator(ticker.MultipleLocator(0.10))
ax.yaxis.set_label_position('right')
ax.yaxis.set_ticks_position('right')
ax.spines["right"].set_position(("axes", 1.0)) # Set second Y-axis 10% away from existing Y axis
# RIGHT Y-Axis labels: twinx sets the same x-axis for both plots / set right y-axis label / set labelcolor and labelsize to the right Y-axis
############################################################################################################################################
ax_1 = ax.twinx()
ax_1.set_ylabel('Wholesalers I/S Ratio', color='blue', size='x-large')
ax_1.tick_params(axis='y', labelcolor='blue',labelsize='large')
# FIRST Right Y-Axis plot: Wholesale IS ratio
#############################################
ax_1.plot(df_ratios.index, df_ratios['whole_is_ratio'], color='blue',linewidth=5.0)
ax_1.spines["right"].set_position(("axes", 1.08)) # Set second Y-axis 10% away from existing Y axis
ax_1.set_ylim(1.15,1.75)
ax_1.yaxis.set_major_locator(ticker.MultipleLocator(0.10))
# SECOND Right Y-Axis: Sum of Mfg+Wholesale ratios
##################################################
ax_2=ax.twinx()
ax_2.set_ylabel('Wholesalers Inventories', color='green', size='x-large')
ax_2.spines["right"].set_position(("axes", 1.18)) # Set second Y-axis 10% away from existing Y axis
ax_2.set_ylim(2.60,3.25)
ax_2.plot(df_ratios.index, df_ratios['totals'], color='green',linewidth=5.0)
ax_2.tick_params(axis='y', labelcolor='green',labelsize='large')
# Show graph:
#############
plt.show()
Here is the result:
How do I get the red y-axis values (manufacturers i/s ratio) to plot on the right side of the graph?
Solution 1:[1]
The problem is, when twinx() is used.
From the source code documentation:
Create a new Axes with an invisible x-axis and an independent y-axis positioned opposite to the original one (i.e. at right).
To reverse this, just use ax.yaxis.tick_right() after all twinx() calls.
For example right before plt.show().
Red ticks should be now placed on the right side.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Domarm |

