Pyplot:在右侧以相同比例添加特定的其他刻度和标签

时间:2018-12-03 16:08:01

标签: python matplotlib

我想在列表“ all_values”的右侧添加刻度和标签。我不想在左侧添加这些值,因为那样会与yaxis上的Basic ticks重叠。如何添加这些值。 这是我的情节中的代码(我根据下面简短而完整的英语注释更改了代码):

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter

materials = {'42CrMo4 nitrocarburiert DNV': {'N_stat': 1e5, 'N_1': 1e5, 'N_D': 2e6, 'S_stat': 1240, 'S_1': 1240, 'S_D': 1030}}       

N_stat_values = []
S_stat_values = []
S_D_values = []

for material in materials:
    N = [1, materials[material]['N_stat'], materials[material]['N_1'], materials[material]['N_D'], 1e10]
    stress = [materials[material]['S_stat'], materials[material]['S_stat'], materials[material]['S_1'], materials[material]['S_D'], materials[material]['S_D']]
    N_stat_values.append(materials[material]['N_stat'])
    S_stat_values.append(materials[material]['S_stat'])
    S_D_values.append(materials[material]['S_D'])    
    plt.loglog(N, stress)

all_values = list(set(S_stat_values + S_D_values))

ax = plt.gca()
ax.yaxis.set_major_formatter(ScalarFormatter())
plt.yticks(np.arange(1000, 1400, 100))
plt.grid(True, which='major',linewidth=0.5)
plt.grid(True, which='minor', linestyle='--', linewidth=0.3)
plt.xlim(np.min(N_stat_values)/10, 1e10)

给出下图: first try

现在,我想在列表all_values的右侧添加刻度和标签。 我试图遵循ImportanceOfBeingErnest的提示:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter

materials = {'42CrMo4 nitrocarburiert DNV': {'N_stat': 1e5, 'N_1': 1e5, 'N_D': 2e6, 'S_stat': 1240, 'S_1': 1240, 'S_D': 1030}}       

N_stat_values = []
S_stat_values = []
S_D_values = []

for material in materials:
    N = [1, materials[material]['N_stat'], materials[material]['N_1'], materials[material]['N_D'], 1e10]
    stress = [materials[material]['S_stat'], materials[material]['S_stat'], materials[material]['S_1'], materials[material]['S_D'], materials[material]['S_D']]
    N_stat_values.append(materials[material]['N_stat'])
    S_stat_values.append(materials[material]['S_stat'])
    S_D_values.append(materials[material]['S_D'])    
    plt.loglog(N, stress)

all_values = list(set(S_stat_values + S_D_values))

ax = plt.gca()
ax.yaxis.set_major_formatter(ScalarFormatter())
ax.set_yticks(np.arange(1000, 1400, 100))
ax2 = ax.twinx()
ax2.set_yscale('log')
ax2.yaxis.set_major_formatter(ScalarFormatter())
ax2.set_yticks(all_values)

plt.grid(True, which='major',linewidth=0.5)
plt.grid(True, which='minor', linestyle='--', linewidth=0.3)
plt.xlim(np.min(N_stat_values)/10, 1e10)

但是我没有得到想要的结果,右侧的刻度线位置错误,x轴上的较小刻度线消失了: Next try

1 个答案:

答案 0 :(得分:0)

使用最新评论,我可以得到所需的输出:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter

materials = {'42CrMo4 nitrocarburiert DNV': {'N_stat': 1e5, 'N_1': 1e5, 'N_D': 2e6, 'S_stat': 1240, 'S_1': 1240, 'S_D': 1030}}       

N_stat_werte = []
S_stat_werte = []
S_D_werte = []

for material in materials:
    N = [1, materials[material]['N_stat'], materials[material]['N_1'], materials[material]['N_D'], 1e10]
    stress = [materials[material]['S_stat'], materials[material]['S_stat'], materials[material]['S_1'], materials[material]['S_D'], materials[material]['S_D']]
    N_stat_values.append(materials[material]['N_stat'])
    S_stat_values.append(materials[material]['S_stat'])
    S_D_values.append(materials[material]['S_D'])    
    plt.loglog(N, stress)

all_values = list(set(S_stat_values + S_D_values))

ax = plt.gca()
ax.yaxis.set_major_formatter(ScalarFormatter())
ax.set_ylim(1000, 1300)
ax.set_yticks(np.arange(1000, 1400, 100))
ax.grid(True, which='major',linewidth=0.5)
ax.grid(True, which='minor', linestyle='--', linewidth=0.3)

ax2 = ax.twinx()
ax2.set_yscale('log')
ax2.yaxis.set_major_formatter(ScalarFormatter())
ax2.set_yticks(all_values)
ax2.set_ylim(1000, 1300)
ax2.grid(True, which='major',linewidth=0.5)
plt.xlim(np.min(N_stat_values)/10, 1e10)
plt.show()

它看起来像这样: final result