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.

4

1 Answer 1

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

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

Comments

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.