如何使用非有序值制作第二个 python x 轴?
How to make second python x-axis with non ordered values?
我想让 axes2 轴按照它们在向量中的方式显示距离向量的值,而不是像默认情况下那样越来越有序。我该怎么做?
代码:
from numpy import linspace
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.axes
import math
distance = [6, 56, 106, 156, 207, 257, 309, 257, 206, 154, 103, 52,
103, 153, 204, 254, 304, 354, 303, 252, 201, 150, 99, 47,
98, 149, 199, 249, 300, 350, 299, 248, 196, 145, 94, 42]
t = linspace(0, 6, 3291)
t1 = linspace(0, 6, 2104)
plt.plot(t, local, "g", label="LOCAL")
plt.plot(t1, cpm, "r", label="CPM")
plt.legend(loc="upper left")
plt.fill_between(t, 0, local, facecolor='green', interpolate=True)
plt.fill_between(t1, 0, cpm, facecolor='red', interpolate=False)
axes1 = plt.gca()
axes2 = axes1.twiny()
axes2.set_xticks(distance, minor=False)
plt.title("Object detection")
plt.xlabel("Time in Minutes")
plt.ylabel("Number of objects")
plt.show()
要在轴上放置所需的值,请使用定位器和格式化程序来放置它们。可以在官方参考中找到here and here
from numpy import linspace
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.axes
import math
import matplotlib.ticker as ticker
np.random.seed(20210210)
local = np.random.randint(0, 20, size=3291)
cpm = np.random.randint(0, 10, size=2104)
distance = [6, 56, 106, 156, 207, 257, 309, 257, 206, 154, 103, 52,
103, 153, 204, 254, 304, 354, 303, 252, 201, 150, 99, 47,
98, 149, 199, 249, 300, 350, 299, 248, 196, 145, 94, 42]
dis = [str(s) for s in distance]
pos = np.arange(0,len(dis))
t = linspace(0, 6, 3291)
t1 = linspace(0, 6, 2104)
plt.figure(figsize=(12,9))
plt.plot(t, local, "g", label="LOCAL")
plt.plot(t1, cpm, "r", label="CPM")
plt.legend(loc="upper left")
plt.fill_between(t, 0, local, facecolor='green', interpolate=True)
plt.fill_between(t1, 0, cpm, facecolor='red', interpolate=False)
axes1 = plt.gca()
axes2 = axes1.twiny()
# axes2.set_xticks(distance, minor=False)
axes2.xaxis.set_major_locator(ticker.FixedLocator(pos))
axes2.xaxis.set_major_formatter(ticker.FixedFormatter(dis))
axes2.set_xlim(0,len(dis))
plt.title("Object detection")
plt.xlabel("Time in Minutes")
plt.ylabel("Number of objects")
plt.show()
我想让 axes2 轴按照它们在向量中的方式显示距离向量的值,而不是像默认情况下那样越来越有序。我该怎么做?
代码:
from numpy import linspace
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.axes
import math
distance = [6, 56, 106, 156, 207, 257, 309, 257, 206, 154, 103, 52,
103, 153, 204, 254, 304, 354, 303, 252, 201, 150, 99, 47,
98, 149, 199, 249, 300, 350, 299, 248, 196, 145, 94, 42]
t = linspace(0, 6, 3291)
t1 = linspace(0, 6, 2104)
plt.plot(t, local, "g", label="LOCAL")
plt.plot(t1, cpm, "r", label="CPM")
plt.legend(loc="upper left")
plt.fill_between(t, 0, local, facecolor='green', interpolate=True)
plt.fill_between(t1, 0, cpm, facecolor='red', interpolate=False)
axes1 = plt.gca()
axes2 = axes1.twiny()
axes2.set_xticks(distance, minor=False)
plt.title("Object detection")
plt.xlabel("Time in Minutes")
plt.ylabel("Number of objects")
plt.show()
要在轴上放置所需的值,请使用定位器和格式化程序来放置它们。可以在官方参考中找到here and here
from numpy import linspace
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.axes
import math
import matplotlib.ticker as ticker
np.random.seed(20210210)
local = np.random.randint(0, 20, size=3291)
cpm = np.random.randint(0, 10, size=2104)
distance = [6, 56, 106, 156, 207, 257, 309, 257, 206, 154, 103, 52,
103, 153, 204, 254, 304, 354, 303, 252, 201, 150, 99, 47,
98, 149, 199, 249, 300, 350, 299, 248, 196, 145, 94, 42]
dis = [str(s) for s in distance]
pos = np.arange(0,len(dis))
t = linspace(0, 6, 3291)
t1 = linspace(0, 6, 2104)
plt.figure(figsize=(12,9))
plt.plot(t, local, "g", label="LOCAL")
plt.plot(t1, cpm, "r", label="CPM")
plt.legend(loc="upper left")
plt.fill_between(t, 0, local, facecolor='green', interpolate=True)
plt.fill_between(t1, 0, cpm, facecolor='red', interpolate=False)
axes1 = plt.gca()
axes2 = axes1.twiny()
# axes2.set_xticks(distance, minor=False)
axes2.xaxis.set_major_locator(ticker.FixedLocator(pos))
axes2.xaxis.set_major_formatter(ticker.FixedFormatter(dis))
axes2.set_xlim(0,len(dis))
plt.title("Object detection")
plt.xlabel("Time in Minutes")
plt.ylabel("Number of objects")
plt.show()