matplotlib - PYTHON: Annotate boxes disapear dependant on range -


i have issue plotting info boxes of data , fit. toy example follows.

import numpy np import matplotlib.pyplot plt  #works sigma = 0.12 mu = 0.5  #half works sigma = 0.1 mu = 0.3  ##none works #sigma = 0.05 #mu = 0.2  sample = np.random.normal(mu, sigma, 10000)  fighs = plt.figure()  axhs = fighs.add_subplot(1, 1, 1) n, histbins, patches = axhs.hist(sample,100, histtype='step', normed=1)  axhs.annotate("$\mu=%.3f$ \n $\sigma=%.3f$ \n $n=%i$"%               (sample.mean(), sample.std(), len(sample)),               xytext=(0.8, 0.85), bbox=dict(boxstyle="sawtooth", fc="w"),               xy=(0.8, 0.85), textcoords='axes fraction') axhs.plot(histbins, plt.mlab.normpdf(histbins, mu, sigma)) axhs.annotate(" $\mu=%.3f$ \n $\sigma=%.3f$ \n $n=%s$"%(mu,sigma,"--"),               xytext=(0.6, 0.85), bbox=dict(boxstyle="round", fc="w"),               xy=(0.6, 0.85), textcoords='axes fraction') plt.show() 

if try 3 different input values, can see has x-range. handled differently below 1 or takes range wrong object. since toy example more constrained in actual use case please not suggest in different way unless values "histbins" passed in similar way.

the desired result achieved first input value. enter image description here

but not second input value: enter image description here

or third input value: enter image description here

this issue discussed before, see

def anno_example(sigma, mu):     sample = np.random.normal(mu, sigma, 10000)      fighs = plt.figure(figsize=(9,6))      axhs = fighs.add_subplot(1, 1, 1)     n, histbins, patches = axhs.hist(sample,100, histtype='step', normed=1)       axhs.plot(histbins, plt.mlab.normpdf(histbins, mu, sigma))      axhs.annotate("$\mu=%.3f$ \n $\sigma=%.3f$ \n $n=%i$"%                   (sample.mean(), sample.std(), len(sample)),                   xytext=(0.8, 0.85), bbox=dict(boxstyle="sawtooth", fc="w"),                   xy=(0.8, 0.85), xycoords=axhs.transaxes)     axhs.annotate(" $\mu=%.3f$ \n $\sigma=%.3f$ \n $n=%s$"%(mu,sigma,"--"),                   xytext=(0.6, 0.85), bbox=dict(boxstyle="round", fc="w"),                   xy=(0.6, 0.85), xycoords=axhs.transaxes)  anno_example(0.12, 0.5) anno_example(0.1,  0.3) anno_example(0.05, 0.2) 

enter image description here enter image description here enter image description here

just replace textcoords="axes fraction" xycoords=axhs.transaxes or xycoords="axes fraction" , works.


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -