5

I am trying to plot multiple features which have different ranges on two y axis. Each axis might contain more than one feature. Code snippet below includes object "Prin Balances" which is a df which contains datatypes float indexed by dates. "Delinquent States" is a list containing a subset of the column headers of Prin Balances.

Delinquent_States = ['1 Mos','2 Mos','3 Mos','> 3 Mos']
fig, ax = plt.subplots()
plt.plot(Prin_Balances['UPB'], '--r', label='UPB')
plt.legend()
ax.tick_params('Bal', colors='r')

# Get second axis
ax2 = ax.twinx()
plt.plot(Prin_Balances[Delinquent_States],  label=Delinquent_States)
plt.legend()
ax.tick_params('vals', colors='b')

My output needs to be cleaned up, esp the legends.

enter image description here

Any suggestions welcomed.

1
  • You can plot it all in one image, but the lines from 0 to 0.05 will of course be too much far from the others. Best way is to plot two subplots. Commented Jul 18, 2017 at 0:26

1 Answer 1

15

As simple as:

import pandas
import matplotlib.pyplot as plt
import random

# Generate some random data
df = pandas.DataFrame({'a': [random.uniform(0,0.05) for i in range(15)], 
                       'b': [random.uniform(0,0.05) for i in range(15)], 
                       'c': [random.uniform(0.8,1) for i in range(15)],
                       'd': [random.uniform(0.8, 1) for i in range(15)],
                       'e': [random.uniform(0.8, 1) for i in range(15)]})
plt.plot(df)

Returns:

Plots

I would suggest however plotting them separately:

fig, ax = plt.subplots(nrows=2,ncols=1)
plt.subplot(2,1,1)
plt.plot(df['a'], 'r', label='Line a')
plt.legend()

plt.subplot(2,1,2)
plt.plot(df['b'], 'b', label='Line b')
plt.legend()

Which yelds:

enter image description here

ADDED:

You can set different scales for each side of the plot:

fig, ax = plt.subplots()
plt.plot(df['a'], '--r', label='Line a')
plt.plot(df['b'], '--k', label='Line b')
plt.legend()
ax.tick_params('vals', colors='r')

# Get second axis
ax2 = ax.twinx()
plt.plot(df['c'], '--b', label='Line c')
plt.plot(df['d'], '--g', label='Line d')
plt.plot(df['e'], '--c', label='Line e')
plt.legend()
ax.tick_params('vals', colors='b')

Not the prettiest, but you get the point.

enter image description here

Sign up to request clarification or add additional context in comments.

7 Comments

Yes, but how to plot multiple lines with different ranges (per my question) on the same graph with 2 axis? Your examples are useful but do not answer my question - unless I am missing something?
There you go. See if this is what you are looking for @GPB
Almost - how do I plot multiple lines with different colours on each axis?
Well, isn't that what the code is doing? Each axis refers to a line. The red line is scaled according to the left-most axis, and the blue line is scaled according to the right-most axis.
I take it your question is rhetorical? :-). I've added an update to my question, I cannot figure out how to use a list to label the legend for multiple lines.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.