将浮点数的精度定义为python绘图上的数据点

Posted

技术标签:

【中文标题】将浮点数的精度定义为python绘图上的数据点【英文标题】:defining the precision of floating numbers as data points on a plot in python 【发布时间】:2017-05-28 08:53:27 【问题描述】:

这就是我想要做的。我有以下代码:

# -*- 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('$\sqrtR [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.\sqrtm]$')
title(r'Fracture toughness $(K_Ic)$ as a function of square root of mean particle radius $\sqrtR$', 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.如何更改绘图上浮点数的精度?我希望只有 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)))]

它确实可以完成这项工作,但对我来说似乎不是很直观。 所以问题是:还有其他方法可以做同样的事情吗? 例如,如何为整个脚本定义浮动精度?

谢谢

【问题讨论】:

关于 SO 的问题旨在针对一个问题。您不能发布一些代码并要求解决您遇到的所有问题。如果您有 n 个不同的问题,请提出 n 个不同的问题,但不要使用相同的代码,而是为每个单独的问题提供 minimal reproducible example。我已将此问题标记为 3 个不同问题的重复项,即您在此处提出的 3 个问题。 @ImportanceOfBeingErnest ,感谢您的评论,实际上我之前看过您介绍的其他问题。我的情况却完全不同。无论如何感谢您的链接 @ImportanceOfBeingErnest,我编辑了我的问题并以更精确的方式对其进行了排序。希望这种方式更清楚。 我在下面的回答还向您展示了minimal reproducible example 的外观;因为没有人关心你拥有的子图、实际值、一些数据文件或其他任何使这 100 行长的东西,而不是可重现的。请注意,我现在确实为您提供了答案,因为这是您在这里的第一个问题,但一般来说,如果您不坚持How to Ask,您将无法在这里解决您的问题。 【参考方案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()

【讨论】:

以上是关于将浮点数的精度定义为python绘图上的数据点的主要内容,如果未能解决你的问题,请参考以下文章

为啥编译器将浮点数的位数固定为 6?

如何将浮点数转换为长度为 4 的字节数组(char* 数组)?

在 C 中,如何将浮点数或双精度数除以 2 的 i 次幂?

将浮点数转换为字符串[重复]

Python如何将浮点数作为十六进制转换为十进制

java - 如何将浮点数转换为 BigDecimal? [复制]