0

I have some data that cant be reproduced but its just a simple read CSV file:

pumpDf = pd.read_csv('C:\\Python Scripts\\data.csv', index_col='Date', parse_dates=True)


pumpDf = pumpDf.truncate(before='12/17/2017', after='12/31/2017')


print(pumpDf.head())
print(pumpDf.tail())

                             DP   pump30
Date
2017-12-17 00:00:07.238  9.9969  81.9101
2017-12-17 00:00:07.255  9.9969  81.9101
2017-12-17 00:00:07.275  9.9969  81.9101
2017-12-17 00:00:07.292  9.9861  81.9101
2017-12-17 00:00:07.311  9.9861  82.2360

I am trying to plot the two data points on separate y axis. Any idea how I can modify this code?

import matplotlib.pyplot as plt

pumpDf.plot()
plt.show()

enter image description here

I am trying to use this code from matplotlib.org to do that task, but I am confused on how I can incorporate my pandas data frame instead of the numpy array. # Create some mock data This code is copied from here

matplotlib.org/devdocs/gallery/api/two_scales

import numpy as np
import matplotlib.pyplot as plt


def two_scales(ax1, time, data1, data2, c1, c2):
    """

    Parameters
    ----------
    ax : axis
        Axis to put two scales on

    time : array-like
        x-axis values for both datasets

    data1: array-like
        Data for left hand scale

    data2 : array-like
        Data for right hand scale

    c1 : color
        Color for line 1

    c2 : color
        Color for line 2

    Returns
    -------
    ax : axis
        Original axis
    ax2 : axis
        New twin axis
    """
    ax2 = ax1.twinx()

    ax1.plot(time, data1, color=c1)
    ax1.set_xlabel('time (s)')
    ax1.set_ylabel('exp')

    ax2.plot(time, data2, color=c2)
    ax2.set_ylabel('sin')
    return ax1, ax2


# Create some mock data
t = np.arange(0.01, 10.0, 0.01)
s1 = np.exp(t)
s2 = np.sin(2 * np.pi * t)

# Create axes
fig, ax = plt.subplots()
ax1, ax2 = two_scales(ax, t, s1, s2, 'r', 'b')


# Change color of each axis
def color_y_axis(ax, color):
    """Color your axes."""
    for t in ax.get_yticklabels():
        t.set_color(color)
    return None
color_y_axis(ax1, 'r')
color_y_axis(ax2, 'b')
plt.show()
Grr
  • 15,553
  • 7
  • 65
  • 85
bbartling
  • 3,288
  • 9
  • 43
  • 88
  • Possible duplicate of [Pandas: Bar-Plot with two bars and two y-axis](https://stackoverflow.com/questions/24183101/pandas-bar-plot-with-two-bars-and-two-y-axis) – DavidG Jan 04 '18 at 16:08
  • Although the dupe uses a bar plot, the solution is still applicable – DavidG Jan 04 '18 at 16:09

3 Answers3

1

From the docs:

pumpDf.DP.plot()
pumpDf.pump30.plot(secondary_y=True)
DavidG
  • 24,279
  • 14
  • 89
  • 82
RSHAP
  • 2,337
  • 3
  • 28
  • 39
1

Given your example data it is quite easy to use either the the DataFrame.plot or pyplot.plot method. The trick is to use a twin Axes with ax.twinx.

import matplotlib.pyplot as plt

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

df.DP.plot(ax=ax)
df.pump30.plot(ax=ax2, color='r')

fig.show()

enter image description here

The same image can also be created like so:

import matplotlib.pyplot as plt

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

ax.plot(df.index, df.DP)
ax2.plot(df.index, df.pump30, color='r')

fig.show()
Grr
  • 15,553
  • 7
  • 65
  • 85
0

This is well-documented here: https://matplotlib.org/examples/api/two_scales.html

The cond snippet they show is:

import numpy as np
import matplotlib.pyplot as plt

fig, ax1 = plt.subplots()
t = np.arange(0.01, 10.0, 0.01)
s1 = np.exp(t)
ax1.plot(t, s1, 'b-')
ax1.set_xlabel('time (s)')
# Make the y-axis label, ticks and tick labels match the line color.
ax1.set_ylabel('exp', color='b')
ax1.tick_params('y', colors='b')

ax2 = ax1.twinx()
s2 = np.sin(2 * np.pi * t)
ax2.plot(t, s2, 'r.')
ax2.set_ylabel('sin', color='r')
ax2.tick_params('y', colors='r')

fig.tight_layout()
plt.show()
craigtopia
  • 21
  • 4