Skip to content Skip to sidebar Skip to footer

Matplotlib: Two Plots On The Same Axes With Different Left Right Scales

I am trying to use this example: http://matplotlib.org/examples/api/two_scales.html#api-two-scales with my own dataset as shown below: import sys import numpy as np import matplot

Solution 1:

try

x1.plot(x,np.array(data))  # do you mean "array" here?

in both places, instead of

x1.plot(x,np.arange(data))

But why do you want to use anything here at all? If you just

x1.plot(data) 

it will generate your x values automatically, and matplotlib will handle a variety of different iterables without converting them.

You should supply an example that someone else can run right away by adding some sample data. That may help you debug also. It's called a Minimal, Complete, and Verifiable Example.

You can get rid of some of that script too:

import matplotlib.pyplot as plt

things = ['2,3', '4,7', '4,1', '5,5']

da, oda = [], []
for thing in things:
    a, b = thing.split(',')
    da.append(a)
    oda.append(b)

fig, ax1 = plt.subplots()
ax2      = ax1.twinx()

ax1.plot(da)
ax2.plot(oda)

plt.savefig("da oda")  # save to a .png so you can paste somewhere
plt.show()

note 1: matplotlib is generating the values for the x axis for you as default. You can put them in if you want, but that's just an opportunity to make a mistake.

note 2: matplotlib is accepting the strings and will try to convert to numerical values for you.

if you want to embrace python, use a list comprehension and then zip(*) - which is the inverse of zip():

import matplotlib.pyplot as plt

things = ['2,3', '4,7', '4,1', '5,5']

da, oda = zip(*[thing.split(',') for thing in things])

fig, ax1 = plt.subplots()
ax2      = ax1.twinx()

ax1.plot(da)
ax2.plot(oda)

plt.savefig("da oda")  # save to a .png so you can paste somewhere
plt.show()

But if you really really want to use arrays, then transpose - array.T - does what zip(*) does will also work for nested iterables. However you should convert to numerical values first using int() or float():

import matplotlib.pyplot as plt
import numpy as np

things = ['2,3', '4,7', '4,1', '5,5']

strtup = [thing.split(',') for thing in things]
inttup = [(int(a), int(b)) for a, b  in strtup]

da, oda = np.array(inttup).T

fig, ax1 = plt.subplots()
ax2      = ax1.twinx()

ax1.plot(da)
ax2.plot(oda)

plt.savefig("da oda")
plt.show()

enter image description here


Post a Comment for "Matplotlib: Two Plots On The Same Axes With Different Left Right Scales"