ticker.FixedFormatter策略显示ax2轴的抽搐

时间:2018-02-28 18:40:41

标签: python matplotlib graph

给出3个阵列:

X1 = 10.00, 30.10, 50.20, 70.30 ...

X2 = 1.9976433815311, 2.0109630315475, 2.0372702369401, 2.0665284897891 ...

Y = -0.0000008764356, -0.0000149459573, -0.0000326996870, -0.0000513717121 ...

X1X2Y之间存在一对一的对应关系,即

X1的第i个元素的第i个关联值为X2,第i个值为Y

以下是Y作为X1(蓝点)函数的图。

  • 我需要X2轴来显示每个X2值的所有相应X1值。

关注second answer on this post

我已经部分完成了ticker.FixedFormatter战略, 通过它:X2数组需要转换为元组,并且此元组的每个元素都需要是一个字符串。

可以看出,X2的每个值都不会显示X1的所有红色值,例如对于X1 = 10.0,相应的X2 = 2.00似乎已被取代。

我不太清楚为什么会这样。如果你能帮助我,我将不胜感激。

enter image description here

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import sys

X1 = np.array([10.0000000000000, 30.1000000000000, 50.2000000000000, 70.3000000000000, 90.4000000000000, 110.5100000000000, 130.6100000000000, 150.7100000000000, 170.8100000000000, 190.9100000000000, 211.0100000000000, 231.1100000000000, 251.2100000000000, 271.3100000000000, 291.4100000000000, 311.5200000000000, 331.6200000000000 ])

Y = np.array([-0.0000008764356, -0.0000149459573, -0.0000326996870, -0.0000513717121, -0.0000652350399, -0.0000842214902, -0.0001003825474, -0.0001214363281, -0.0001376971422, -0.0001572720132, -0.0001971891337, -0.0002203926200, -0.0002747064193, -0.0003217228112, -0.0003764577474, -0.0004657478828, -0.0006232016207])

X2 = np.array([1.9976433815311, 2.0109630315475, 2.0372702369401, 2.0665284897891, 2.0995743328944, 2.1392386324550, 2.1789200955649, 2.2290243968267, 2.2872281293691, 2.3180577547912, 2.4100643103912, 2.4826981368480, 2.5794602952095, 2.6764219232389, 2.7963983991814, 2.9740753305878, 3.3107035136072])


##### Plotting:
fig, ax1 = plt.subplots()
ax1.plot(X1, Y, linestyle='--', marker="o",  markersize=6, color='blue')
ax1.set_ylabel('Y', fontsize=20)

# Make the ax1-ticks and ax1-tick-labels match the line color (blue):
ax1.set_xlabel('X1', fontsize=20, color='blue')
plt.setp(ax1.get_xticklabels(), rotation='45') # rotate them

# Create a new axis:
ax2 = ax1.twiny()

# Make the ax2-ticks and ax2-tick-labels match the red color: 
ax2.set_xlabel('X2', fontsize=20, color='red')
ax2.tick_params('x', colors='red')

fig.tight_layout()
ax2.set_xlim(1.9, 3.4)
ax1.set_ylim(-0.0007, 1.1e-5)
ax2.set_ylim(-0.0007, 1.1e-5)
ax1.grid()

# Convert all X2 elements to a list of strings:
X2_string_all = []
for i in X2:
  aux = "%.2f" % i
  X2_string = str(aux)
  X2_string_all.append(X2_string)

# Convert that list into a tuple:
X2_string_all_tuple = tuple(X2_string_all)

ax1.xaxis.set_major_locator(ticker.FixedLocator((X1)))
ax2.xaxis.set_major_formatter(ticker.FixedFormatter((X2_string_all_tuple)))

plt.show()

这样的东西将是理想的情节(没有必要遇到情节的红线):

enter image description here

1 个答案:

答案 0 :(得分:1)

在您的代码ax2中,不知道它应该与ax1完全相同,只是使用不同的标签。所以你需要告诉它,

ax2.set_xlim(ax1.get_xlim())

然后只使用两个轴的相同刻度位置

ax1.set_xticks(X1)
ax2.set_xticks(X1)

并使用ax2

中的值标记X2的刻度
ax2.set_xticklabels(["%.2f" % i for i in X2])

完整代码:

import numpy as np
import matplotlib.pyplot as plt

X1 = np.array([10., 30.1, 50.2, 70.3, 90.4, 110.510, 130.610, 150.710, 170.810, 
               190.910, 211.010, 231.110, 251.210, 271.310, 291.410, 311.52, 331.62])

Y = np.array([-0.00000087, -0.0000149, -0.0000326, -0.0000513, -0.00006523, -0.0000842,
              -0.0001003, -0.0001214,  -0.00013769, -0.0001572, -0.0001971, -0.0002203, 
              -0.00027470, -0.0003217, -0.0003764, -0.0004657,  -0.00062320])

X2 = np.array([1.997, 2.0109, 2.0372, 2.0665, 2.099, 2.1392, 2.1789, 2.2290, 
               2.287, 2.3180, 2.4100, 2.4826, 2.579, 2.6764, 2.7963, 2.9740, 3.310])

##### Plotting:
fig, ax1 = plt.subplots()
ax1.grid()
ax2 = ax1.twiny()

ax1.plot(X1, Y, linestyle='--', marker="o",  markersize=6, color='blue')
ax1.set_ylabel('Y', fontsize=20)
ax1.set_xlabel('X1', fontsize=20, color='blue')
plt.setp(ax1.get_xticklabels(), rotation='45') # rotate them 
ax2.set_xlabel('X2', fontsize=20, color='red')
plt.setp(ax2.get_xticklabels(), rotation='45', color='red')

# Set xlimits of ax2 the same as ax1
ax2.set_xlim(ax1.get_xlim())
# Set ticks at desired position
ax1.set_xticks(X1)
ax2.set_xticks(X1)
# Label ticks of ax2 with values from X2
ax2.set_xticklabels(["%.2f" % i for i in X2])

fig.tight_layout()
plt.show()

enter image description here