Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

matplotlib twinx legend

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
rc('mathtext', default='regular')

time = np.arange(10)
temp = np.random.random(10)*30
Swdown = np.random.random(10)*100-10
Rn = np.random.random(10)*100-10

fig = plt.figure()
ax = fig.add_subplot(111)

lns1 = ax.plot(time, Swdown, '-', label = 'Swdown')
lns2 = ax.plot(time, Rn, '-', label = 'Rn')
ax2 = ax.twinx()
lns3 = ax2.plot(time, temp, '-r', label = 'temp')

# added these three lines
lns = lns1+lns2+lns3
labs = [l.get_label() for l in lns]
ax.legend(lns, labs, loc=0)

ax.grid()
ax.set_xlabel("Time (h)")
ax.set_ylabel(r"Radiation ($MJ,m^{-2},d^{-1}$)")
ax2.set_ylabel(r"Temperature ($^circ$C)")
ax2.set_ylim(0, 35)
ax.set_ylim(-20,100)
plt.show()
Comment

PREVIOUS NEXT
Code Example
Python :: depth first search in python 
Python :: remove first item form dictionary python 
Python :: tuple comprehension python 
Python :: how to find the transpose of a matrix in python 
Python :: python string trim 
Python :: mechanize python 
Python :: absolute value in python 
Python :: sns how to change color if negative or positive 
Python :: transform data frame in list 
Python :: how to create python environment 
Python :: -- python 
Python :: python get line number x in file 
Python :: time difference between two datetime.time 
Python :: for loop from n to 1 in python 
Python :: dataframe to pandas 
Python :: python advanced programs time module 
Python :: how to take first digit of number python 
Python :: Math Module ceil() Function in python 
Python :: how to calculate fibonacci numbers in python 
Python :: beautifulsoup find 
Python :: python sys.argv 
Python :: delete occurrences of an element if it occurs more than n times python 
Python :: length of list python 
Python :: python save button 
Python :: ordered dictionary 
Python :: how to convert pandas series to 2d numpy array 
Python :: merge two query sets django 
Python :: list get every 2nd element 
Python :: how to print all items in a list python 
Python :: numpy linspace of dates 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =