0

I have a pandas series X as X1, X2, ... Xn

I normalize the X series to have a new Y series with mean=0 and std=1.

I want to plot the histogram of X with 2 xticks, one is original values and the other represented normalized values.

How could I do that with matplotlib?

Update:

import numpy as np

x = np.random.randint (0,100,1000)

y = (x- np.mean(x))/np.std(x)

Now I want to plot the histogram of y, but also show the original values (x), not only values of y.

DavidG
  • 24,279
  • 14
  • 89
  • 82
mommomonthewind
  • 4,390
  • 11
  • 46
  • 74
  • 1
    Share your data series sufficient enough to reproduce/plot something. Currently we have no data to plot and it's all speculative. – Sheldore Sep 03 '18 at 08:59
  • 1
    Can you show an example of what the plot should look like? Preferably you should also include a [mcve] – DavidG Sep 03 '18 at 08:59
  • 1
    https://matplotlib.org/examples/api/two_scales.html – taras Sep 03 '18 at 09:14
  • Possible duplicate of [How to add a second x-axis in matplotlib](https://stackoverflow.com/questions/10514315/how-to-add-a-second-x-axis-in-matplotlib) – DavidG Sep 03 '18 at 09:18

1 Answers1

1

Here is an example, with a second scaled top axis:

import numpy as np
import matplotlib.pyplot as plt

# Some data
x = 12 + 3*np.random.randn(1000)
x_normed = (x - np.mean(x))/np.std(x)

# Graph
fig, ax1 = plt.subplots()

ax1.hist(x_normed, bins=20)

x1_lim = np.array(ax1.get_xlim())
x2_lim = x1_lim*np.std(x) + np.mean(x)

ax2 = ax1.twiny()
ax2.set_xlim(x2_lim)

ax1.set_ylabel('count')
ax1.set_xlabel('normed x', color='k')
ax2.set_xlabel('x', color='k');

Doing the other way around is I think better:

import numpy as np
import matplotlib.pyplot as plt

# Some data
x = 12 + 3*np.random.randn(1000)

# Graph
fig, ax1 = plt.subplots()

ax1.hist(x, bins=20)

x1_lim = np.array(ax1.get_xlim())
x2_lim = (x1_lim - np.mean(x))/np.std(x)

ax2 = ax1.twiny()
ax2.set_xlim(x2_lim)

ax1.set_ylabel('count')
ax1.set_xlabel('x', color='k')
ax2.set_xlabel('x normed', color='k');

which gives:

hist_with_multiple_axis

xdze2
  • 3,986
  • 2
  • 12
  • 29