Search code examples
pythonmatplotlibplot

How can I add spines to the left of a figure in matplotlib?


I'm trying to plot the DATE on the X-Axis, and then Temperature, Humidity, Pressure, and UV-Index on 4 Y-axes (same figure-no subplots)

I'd like for the spine and legend for the Temperature to be on the left most side of the window, followed by the Humidity to its right (still left of the plot window). The Pressure and UV spines/legends would be on the right side of the plot area

   T H [ plots ] P  UV

But I can't get set_position to place the Temperature and legend to be left of the plot window

axis: axTemp = axHumidity.twinx() 
axTemp.spines['left'].set_position(('axes',0.01)) # places new spine to the right of axHumidity (inside the plot area) 
axTemp.spines['left'].set_position(('axes',-1)) # places new spine to the right of the axHumidity

I understand one can add an additional spine (with new scaling), but only if it shares the same data such that there is a function between the 2 scales/units..

ax2.secondary_yaxis(-0.25,  function=

in my case, its different data so the scale/range/legend comes directly from the data itself. How can I move the Temperature Spine/Legend to be on the far left, and the Humidity legend to be left of the Humidity spine?

import matplotlib.pyplot as plt
from matplotlib.pyplot import figure

# csv file
dates = [1,2,3,4]
humidity = [-4,0,0,-40]
pressure = [15,30,10,25]
temperature = [67,68,69,70]
uv = [1,0, -1,0]

# layout  :  TEMP (scale/legends)  HUMIDITY   --- FIGURES ---   PRESSURE  UV
fig, ax1 = plt.subplots()
fig.set_size_inches(14.5, 8.0)
fig.subplots_adjust(right=.6)

# Humidity
ax1.plot(dates,humidity,color="blue", label='ax1-humidity-data')
ax1.legend(loc='upper left', bbox_to_anchor=(-0.5, 1),  ncol=2, borderaxespad=0)  ### ERROR: Won't move to left of plot window
ax1.tick_params(axis='y',colors="black")   
ax1.set_ylabel("Ax1 Humidity Spine",color="black")  

# Pressure
ax2 = ax1.twinx()
ax2.plot(dates,pressure,color="green", label='ax2-pressure-data')
ax2.legend(loc='upper right', bbox_to_anchor=(1.35, 1),  ncol=2, borderaxespad=0)
ax2.set_ylabel("Ax2 Pressure Spine",color="black")
ax2.tick_params(axis='y',colors="black")
ax2.spines['right'].set_color("black")   

# Temperature
ax3 = ax1.twinx()
ax3.plot(dates,temperature,color="red",  label='ax3-temp-data')
ax3.legend(loc='upper left', bbox_to_anchor=(-1,1),  ncol=2, borderaxespad=0) ### ERROR: Won't move to left of Humidity
ax3.set_ylabel("Ax3 Temp Spine",color="black")
ax3.spines['left'].set_position(('axes',-1))   ### ERROR: Won't move to left of Humidity
ax3.tick_params(axis='y',colors="black")


# UV
ax4 = ax1.twinx()
ax4.plot(dates,uv,color="yellow",  label='ax4-uv-data')
ax4.legend(loc='upper right', bbox_to_anchor=(1.75, 1),  ncol=2, borderaxespad=0)
ax4.set_ylabel("Ax4 Uv Spine",color="black")
ax4.tick_params(axis='y',colors="black")
ax4.spines['right'].set_position(('axes',1.5))  

plt.show()

enter image description here


Solution

  • Is the below adequate? Doesn't exactly match your description, but might serve the purpose.

    enter image description here

    import matplotlib.pyplot as plt
    
    # csv file
    dates = [1, 2, 3, 4]
    humidity = [-4, 0, 0, -40]
    pressure = [15, 30, 10, 25]
    temperature = [67, 68, 69, 70]
    uv = [1, 0, -1, 0]
    
    # layout  :  TEMP (scale/legends)  HUMIDITY   --- FIGURES ---   PRESSURE  UV
    fig, ax1 = plt.subplots(figsize=(5, 5))
    
    # Humidity
    ax1.plot(dates, humidity, color="blue", label='ax1-humidity-data')
    ax1.set_ylabel("Ax1 Humidity Spine")
    
    # Pressure
    ax2 = ax1.twinx()
    ax2.plot(dates, pressure, color="tab:green", label='ax2-pressure-data')
    ax2.set_ylabel("Ax2 Pressure Spine")
    
    # Temperature
    ax3 = ax1.twinx()
    ax3.plot(dates, temperature, color="tab:red",  label='ax3-temp-data')
    ax3.set_ylabel("Ax3 Temp Spine")
    ax3.spines['right'].set_position(('axes', -0.4))
    
    # UV
    ax4 = ax1.twinx()
    ax4.plot(dates, uv, color="tab:orange", label='ax4-uv-data')
    ax4.set_ylabel("Ax4 Uv Spine")
    ax4.spines['right'].set_position(('axes',1.3))  
    
    fig.legend(ncol=4)
    plt.show()