Im plotting some values in a histogram and want to scale the y-axis but I only find ways to either normalize the y-axis values or to scale them logarithmically. My values are in 100ps timesteps and I want to multiply every y-axis value by 0.1 to get to a nice and easier to understand ns step size.
How can I scale the y-axis values in a histogram ?
n, bins, patches = plt.hist(values1, 50, facecolor='blue', alpha=0.9, label="Sample1",align='left')
n, bins, patches = plt.hist(values2, 50, facecolor='red', alpha=0.9, label="Sample2",align='left')
plt.xlabel('value')
plt.ylabel('time [100ps]')
plt.title('')
plt.axis([-200, 200, 0, 180])
plt.legend()
plt.show()
In this graph 10 on the y axis means 1ns:
The easiest way to do it is to define your axis and then multiply the ticks of your axis. For example
fig, ax1 = plt.subplots()
ax1.hist(values1, 50, facecolor='blue', alpha=0.9, label="Sample1",align='left')
y_vals = ax1.get_yticks()
ax1.set_yticklabels(['{:3.0f}%'.format(x * 100) for x in y_vals])
plt.show()
The way I would solve is very simple: just multiply your arrays values1 and values2 by 0.1 before plotting it.
The reason why log-scaling exists in matplotlib is that log-transformations are very common. For simple multiplicative scaling, it is very easy to just multiply the array(s) that you are plotting.
EDIT: You're right, I was wrong and confused (did not noticed you were dealing with an histogram). What I would do then is use the matplotlib.ticker
module to adjust the ticks on your y-axis. See below:
# Your code.
n, bins, patches = plt.hist(values1, 50, facecolor='blue', alpha=0.9, label="Sample1",align='left')
n, bins, patches = plt.hist(values2, 50, facecolor='red', alpha=0.9, label="Sample2",align='left')
plt.xlabel('value')
plt.ylabel('time [100ps]')
plt.title('')
plt.axis([-200, 200, 0, 180])
plt.legend()
# My addition.
import matplotlib.ticker as mtick
def div_10(x, *args):
"""
The function that will you be applied to your y-axis ticks.
"""
x = float(x)/10
return "{:.1f}".format(x)
# Apply to the major ticks of the y-axis the function that you defined.
ax = plt.gca()
ax.yaxis.set_major_formatter(mtick.FuncFormatter(div_10))
plt.show()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.