2017-05-28 69 views
-1

下面是我正在嘗試的操作。我有以下代碼:將浮點數的精度定義爲python中的圖上的數據點

# -*- 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() 

正如你在我的代碼圖中看到4顯示爲這樣: Floating numbers on the plot are too long

1.How我可以改變浮在情節數字的精確度?我想只有3位小數。 這裏是我做過什麼:

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)))] 

它實際上做的工作,但似乎並沒有很直觀的給我。 所以問題是:有沒有其他方法可以做同樣的事情? 例如,我怎樣才能定義整個腳本的浮動精度?

謝謝

+0

上做題是爲了具體到一個問題。你不能發佈一些代碼,並要求解決你所有的問題。如果您有* n *個不同的問題,請問* n *個不同的問題,但不要使用相同的代碼,而應爲每個單獨的問題提供一個[mcve]。我已經標記了3個不同問題的重複問題,這是你在這裏問的3個問題。 – ImportanceOfBeingErnest

+0

@ImportanceOfBeingErnest,感謝您的評論,我實際上以前看過你介紹的其他問題。我的情況是完全不同的。無論如何感謝鏈接 – Dude

+0

@ImportanceOfBeingErnest,我編輯我的問題,並以更精確的方式進行排序。希望這種方式更清楚。 – Dude

回答

1

你不想改變整個腳本的浮點精度。

你想要的只是以一定的精度格式化標籤。所以你只想設置循環中標籤的格式。

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