-1

Here is what I am trying to do. I have the following code:

# -*- coding: utf-8 -*-
from pylab import *
import matplotlib.pyplot as plt
import numpy as np


### processing function
def store(var,textFile):
    data=loadtxt(textFile,skiprows=1)
    it=[]
    eps=[]
    sig=[]
    tc=[]
    sc=[]
    te=[]
    se=[]
    ubf=[]
    for i in range(0,len(data)):
      it.append(float(data[i,1]))
      eps.append(float(data[i,0]))
      sig.append(float(data[i,4]))
      tc.append(float(data[i,6]))
      sc.append(float(data[i,2]))
      te.append(float(data[i,7]))
      se.append(float(data[i,3]))
      ubf.append(float(data[i,8]))
    var.append(it)
    var.append(eps)
    var.append(sig)
    var.append(tc)
    var.append(sc)
    var.append(te)
    var.append(se)
    var.append(ubf)

### data input
dataFile1='0101005_5k_tensionTestCentreCrack_l0.001a0_r0.02'
a1=[]
store(a1,dataFile1)

dataFile2='0101005_10k_tensionTestCentreCrack_l0.001a0_r0.02'
a2=[]
store(a2,dataFile2)

dataFile3='0101005_20k_tensionTestCentreCrack_l0.001a0_r0.01'
a3=[]
store(a3,dataFile3)

dataFile4='0101005_40k_tensionTestCentreCrack_l0.001a0_r0.02'
a4=[]
store(a4,dataFile4)

dataFile5='0101005_80k_tensionTestCentreCrack_l0.001a0_r0.02'
a5=[]
store(a5,dataFile5)

dataFile6='0101005_120k_tensionTestCentreCrack_l0.001a0_r0.02'
a6=[]
store(a6,dataFile6)

dataFile7='0101005_200k_tensionTestCentreCrack_l0.001a0_r0.02'
a7=[]
store(a7,dataFile7)

### plot control
rcParams.update({'legend.numpoints':1,'font.size': 20,'axes.labelsize':25,'xtick.major.pad':10,'ytick.major.pad':10,'legend.fontsize':20})
lw=2
ms=10

### plots




#savefig(dataFile1+'_sigVSeps.eps',dpi=1000,format='eps',transparent=False)






hcl=0.005
###plot of fracture toughness vs. square root of mean radius
A=[0.0023**0.5, 0.0019**0.5, 0.0015**0.5, 0.0012**0.5, 0.0009**0.5, 0.0008**0.5, 0.0007**0.5]
B=[max(a1[2])*((pi*hcl)**0.5) ,max(a2[2])*((pi*hcl)**0.5) ,max(a3[2])*((pi*hcl)**0.5), max(a4[2])*((pi*hcl)**0.5), max(a5[2])*((pi*hcl)**0.5), max(a6[2])*((pi*hcl)**0.5), max(a7[2])*((pi*hcl)**0.5)]
#B=[max(a1[2]),max(a2[2]),max(a3[2]),max(a4[2])]
figure(4,figsize=(12,10))
grid()
xlabel('$\sqrt{R} [m]$')
##axis(xmin=0,xmax=0.1)
plot(A,[x/1e6 for x in B],'-ko',linewidth=lw)  
for xy in zip(A,[x/1e6 for x in B]):                                       
    annotate('(%s, %s)' % xy, xy=xy, textcoords='data') 
ylabel(r'$K_{Ic} [MPa.\sqrt{m}]$')
title(r'Fracture toughness $(K_{Ic})$ as a function of square root of mean particle radius $\sqrt{R}$', fontsize=14, color='blue')




G=[(1.42*1e-5, 8.5*1e-2), (1.19*1e-5, 7.8*1e-2), (1.03*1e-5, 6*1e-2), (8.95*1e-6, 4.7*1e-2), (7.63*1e-6, 3.8*1e-2), (7.12*1e-6, 3.2*1e-2), (5.72*1e-6, 2.6*1e-2)]
PN=[5*1e3, 10*1e3, 20*1e3, 40*1e3, 80*1e3, 120*1e3, 200*1e3]


figure(5,figsize=(12,10))
for PNe, Ge, in zip(PN, G):
    scatter([PNe]*len(Ge), Ge, color=['blue', 'green'])
grid()
xlim(xmin=0, xmax=200000)
#ylim(ymin=0, ymax=1)
xlabel('Number of particles')
ylabel(r'Energy release rate')

figure(6,figsize=(12,10))
ax1=subplot(1,1,1)
grid()
xlabel(r'$\varepsilon_1$ [millistrain]')
#axis(xmin=0,xmax=0.12)
ax1.plot([x*1e3 for x in a1[1]],[x/1e6 for x in a1[2]],'-b',linewidth=lw)
ax1.plot([x*1e3 for x in a2[1]],[x/1e6 for x in a2[2]],'-g',linewidth=lw)
ax1.plot([x*1e3 for x in a3[1]],[x/1e6 for x in a3[2]],'-r',linewidth=lw)
ax1.plot([x*1e3 for x in a4[1]],[x/1e6 for x in a4[2]],'-y',linewidth=lw)
ax1.plot([x*1e3 for x in a5[1]],[x/1e6 for x in a5[2]],'-m',linewidth=lw)
ax1.plot([x*1e3 for x in a6[1]],[x/1e6 for x in a6[2]],'-c',linewidth=lw)
ax1.plot([x*1e3 for x in a7[1]],[x/1e6 for x in a7[2]],'-k',linewidth=lw)
ylabel(r'$\sigma_1$ [MPa]')
#legend(('5k','10k','20k','40k'),2)
#axis(ymin=0,ymax=10)
ax2 = ax1.twinx()
ax2.plot([x*1e3 for x in a1[1]],[x+y for x,y in zip(a1[5],a1[6])],'--b',linewidth=lw)
ax2.plot([x*1e3 for x in a2[1]],[x+y for x,y in zip(a2[5],a2[6])],'--g',linewidth=lw)
ax2.plot([x*1e3 for x in a3[1]],[x+y for x,y in zip(a3[5],a3[6])],'--r',linewidth=lw)
ax2.plot([x*1e3 for x in a4[1]],[x+y for x,y in zip(a4[5],a4[6])],'--y',linewidth=lw)
ax2.plot([x*1e3 for x in a5[1]],[x+y for x,y in zip(a5[5],a5[6])],'--m',linewidth=lw)
ax2.plot([x*1e3 for x in a6[1]],[x+y for x,y in zip(a6[5],a6[6])],'--c',linewidth=lw)
ax2.plot([x*1e3 for x in a7[1]],[x+y for x,y in zip(a7[5],a7[6])],'--k',linewidth=lw)
ylabel('energy released by microcracking [J]')
#axis(ymin=0,ymax=200)




### show or save
show()

As you see in my code figure 4 appears as this: Floating numbers on the plot are too long

1.How can I change the precision of floating numbers on the plot? I would like to have only 3 decimal. Here is what I did:

A=[float("{0:.2e}".format(0.0023**0.5)), float("{0:.2e}".format(0.0019**0.5)), float("{0:.2e}".format(0.0015**0.5)), float("{0:.2e}".format(0.0012**0.5)), float("{0:.2e}".format(0.0009**0.5)), float("{0:.2e}".format(0.0008**0.5)), float("{0:.2e}".format(0.0007**0.5))]
B=[float("{0:.2e}".format(max(a1[2])*((pi*hcl)**0.5))) ,float("{0:.2e}".format(max(a2[2])*((pi*hcl)**0.5))) ,float("{0:.2e}".format(max(a3[2])*((pi*hcl)**0.5))), float("{0:.2e}".format(max(a4[2])*((pi*hcl)**0.5))), float("{0:.2e}".format(max(a5[2])*((pi*hcl)**0.5))), float("{0:.2e}".format(max(a6[2])*((pi*hcl)**0.5))), float("{0:.2e}".format(max(a7[2])*((pi*hcl)**0.5)))]

It actually does the job but doesn't seem very intuitive to me. So the question is : Is there any other way to do the same thing? For instance how can I define the floating precision for my whole script?

Thanks

Dude
  • 191
  • 3
  • 12
  • Questions on SO are meant to be specific to one problem. You cannot post some code and ask to solve all the problems you have with it. If you have *n* different problems, ask *n* different questions, but don't use the same code, instead provide a [mcve] for each individual problem. I've marked this question duplicate of 3 different questions, which are the 3 questions you're asking here. – ImportanceOfBeingErnest May 28 '17 at 09:37
  • @ImportanceOfBeingErnest , Thanks for your comment, I actually previously looked at those other questions you introduced. my case is quite different though. Thanks for the links anyway – Dude May 28 '17 at 11:33
  • @ImportanceOfBeingErnest, I edited my question and sort it in a more precise way. Hope this way is more clear. – Dude May 28 '17 at 11:35
  • 1
    My answer below also shows you how a [mcve] should look like; because nobody cares about the subplots you have, the actual values, some data files or whatever else makes this 100 lines long, without being reproducible. Note that I did provide you with an answer now because this is your first question here, but in general if you don't stick to [ask], you will have problems getting your questions solved here. – ImportanceOfBeingErnest May 28 '17 at 11:58

1 Answers1

2

You don't want to change the floating point precision of the whole script.

What you want is only to format the labels with a certain precision. So you only want to set the format for the labels in the loop.

import numpy as np; np.random.seed(10)
import matplotlib.pyplot as plt

y = np.cumsum(np.random.normal(size=10)+0.2)
x = np.arange(len(y))

fig, ax=plt.subplots()
ax.plot(x,y)

for a,b in zip(x,y):                                       
    ax.annotate('({:d}, {:.2f})'.format(a,b), xy=(a,b), textcoords='data') 

plt.show()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712