Y 축이 다른 서브 플롯으로 그림을 만들고 있습니다. 마지막 플롯의 y 축은 과학적 표기법으로 표시해야하며 축의 숫자가 소수점 2 자리 만 표시하기를 원합니다. 즉 2.1565 대신 2.15입니다. 나는 성공하지 않고 몇 시간 동안 숫자 형식을 지정하려고 노력해 왔습니다. 그것을 수행하는 방법에 대한 제안?
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.ticker as mtick
from matplotlib.ticker import FormatStrFormatter
fig, axs = plt.subplots(2, 3)
y_pos = [0.2,0.4,0.6,0.8]
plt.setp(axs, xticks=[0.2,0.4,0.6,0.8], xticklabels=['A','B','C','D'],xlim=[0.1,0.9],facecolor='#F4F4F4')
#P-2
Pval = [3.3705E+00,3.4262E+00,3.5093E+00,3.4882E+00]
axs[0,0].set_ylim(3.3,3.55)
axs[0,0].set_yticks(np.arange(3.3,3.56, 0.05))
axs[0,0].grid(zorder=0)
axs[0,0].bar(y_pos,Pval,width=0.1,color='#ADDDCE')
axs[0,0].set_title(r'$Initial$')
axs[0,0].grid(True,linestyle='--',color='#D6D8CD')
#P-1
Pval = [1.6667E+00,1.6079E+00,1.6087E+00,1.6132E+00]
axs[0,1].set_ylim(1.5,1.7)
axs[0,1].set_yticks(np.arange(1.5,1.71, 0.05))
axs[0,1].grid(zorder=0)
axs[0,1].bar(y_pos,Pval,width=0.1,color='#FFC347')
axs[0,1].set_title(r'$Test 1$')
axs[0,1].grid(True,linestyle='--',color='#D6D8CD')
#P1
Pval = [9.9458E-01,9.4241E-01,9.4569E-01,9.4014E-01]
axs[0,2].set_ylim(0.8,1)
axs[0,2].set_yticks(np.arange(0.8,1.01, 0.05))
#
axs[0,2].grid(zorder=0)
axs[0,2].bar(y_pos,Pval,width=0.1,color='#FC746C')
#
axs[0,2].set_title(r'$Test 2$')
axs[0,2].grid(True,linestyle='--',color='#D6D8CD')
#P2
Pval = [2.4196E+00,2.3292E+00,2.3499E+00,2.3213E+00]
axs[1,0].set_ylim(1.8,2.5)
axs[1,0].set_yticks(np.arange(1.8,2.7, 0.2))
#
axs[1,0].grid(zorder=0)
axs[1,0].bar(y_pos,Pval,width=0.1,color='#70AE98')
#
axs[1,0].set_title(r'$ \omega $')
axs[1,0].grid(True,linestyle='--',color='#D6D8CD')
#P3
Pval = [1.5646E+01,1.5346E+01,1.5538E+01,1.5298E+01]
axs[1,1].set_ylim(15,15.7)
axs[1,1].set_yticks(np.arange(15.,15.9, 0.2))
#X
axs[1,1].grid(zorder=0)
axs[1,1].bar(y_pos,Pval,width=0.1,color='#F0A35E')
#
axs[1,1].set_title(r'$ \Pi $')
axs[1,1].grid(True,linestyle='--',color='#D6D8CD')
#P4
Pval = [2.1391E+02,2.1148E+02,2.1434E+02,2.1085E+02]
axs[1,2].set_ylim(2.09E+02,2.15E+02)
axs[1,2].set_yticks(np.arange(2.09E+02,2.16E+02,0.95))
#axs[1,2].ticklabel_format(style='sci',scilimits=(200,300),axis='y')
#
#axs[1,2].yaxis.set_major_formatter(MathTextSciFormatter('%.2e'))
#m = grabMaxExponent(2)
#axs[1,2].yaxis.set_major_formatter(OOMFormatter(2, "%1.1f"))
#axs[1,2].yaxis.set_major_formatter(mtick.FormatStrFormatter('%.2f'))
axs[1,2].ticklabel_format(axis='y', style='sci', scilimits=(2,2))
axs[1,2].yaxis.major.formatter._useMathText = True
#axs[1,2].yaxis.set_major_formatter(FormatStrFormatter('%.2f'))
#axs[1,2].yaxis.set_major_formatter(MathTextSciFormatter('%.2e'))
#axs[1,2].ticklabel_format(useOffset=False)
#X
axs[1,2].grid(zorder=0)
axs[1,2].bar(y_pos,Pval,width=0.1,color='#CA7E8D')
#
axs[1,2].set_title(r'$\gamma$')
axs[1,2].grid(True,linestyle='--',color='#D6D8CD')
fig.tight_layout()
plt.show()
ScalarFormatter
사용자 지정 서식을 지원하지 않습니다. 시도 할 수 있는 것은 형식 지정을 담당하는 메서드를 재정의 하는 것입니다.
(참고 : 이것은 Matplotlib 2.0.x가 아닌 Matplotlib 3.0.x-Python 3.x에서 작동합니다.)
from matplotlib.ticker import ScalarFormatter
...
# P4
# Extend ScalarFormatter
class MyScalarFormatter(ScalarFormatter):
# Override '_set_format' with your own
def _set_format(self):
self.format = '%.2f' # Show 2 decimals
# Your data as before
Pval = [2.1391E+02,2.1148E+02,2.1434E+02,2.1085E+02]
axs[1,2].set_ylim(2.09E+02,2.15E+02)
axs[1,2].set_yticks(np.arange(2.09E+02,2.16E+02,0.95))
# New code
custom_formatter = MyScalarFormatter(useMathText=True)
axs[1,2].yaxis.set_major_formatter(custom_formatter)
axs[1,2].yaxis.major.formatter.set_powerlimits((2,2))
#axs[1,2].yaxis.major.formatter.set_scientific(True) # Redundant: True is default
# Rest code as before
axs[1,2].grid(zorder=0)
axs[1,2].bar(y_pos,Pval,width=0.1,color='#CA7E8D')
axs[1,2].set_title(r'$\gamma$')
axs[1,2].grid(True,linestyle='--',color='#D6D8CD')
fig.tight_layout()
plt.show()
그것은 반올림 하지만, 당신의 y 축 값을. 그러나 원하는 결과를 얻기 위해 이런 식으로 약간 해결할 수 있습니다. ScalarFormatter
소스를 살펴보십시오 .
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다