有没有办法在 MATPLOTLIB 中使用两个带有不同标签的轴?
Is there a way to have TWO axis with DIFFERENT labels in MATPLOTLIB?
我正在尝试创建一个图,该图在底部有一个 x 轴,带有基于文本的标签,而我希望在顶部有另一个 x 轴,它具有不同的基于文本的标签。
到目前为止我发现的最接近的东西是 'secondary_axis' (Link),但是当我尝试填写基于文本的标签时,出现 TypeError: unhashable type: numpy.ndarray
。
我根据 this 中的一些代码生成了以下示例:
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
# sphinx_gallery_thumbnail_number = 2
vegetables = ["cucumber", "tomato", "lettuce", "asparagus",
"potato", "wheat", "barley"]
farmers = ["Farmer Joe", "Upland Bros.", "Smith Gardening",
"Agrifun", "Organiculture", "BioGoods Ltd.", "Cornylee Corp."]
vegetables_to_farmers = dict(zip(vegetables, farmers))
farmers_to_vegetables = dict(zip(farmers, vegetables))
harvest = np.array([[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0],
[2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0],
[1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0],
[0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0],
[0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0],
[1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1],
[0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3]])
fig, ax = plt.subplots()
im = ax.imshow(harvest)
# We want to show all ticks...
ax.set_xticks(np.arange(len(farmers)))
ax.set_yticks(np.arange(len(vegetables)))
# ... and label them with the respective list entries
ax.set_xticklabels(farmers)
# Rotate the tick labels and set their alignment.
plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
rotation_mode="anchor")
def vegetables2farmers(x):
return vegetables_to_farmers[x]
def farmers2vegetables(x):
return farmers_to_vegetables[x]
secax = ax.secondary_xaxis('top', functions=(vegetables2farmers, farmers2vegetables))
fig.tight_layout()
plt.show()
最好将蔬菜作为顶部栏中的标签。您知道如何解决这个问题吗?
您将坐标轴刻度的概念与坐标轴标签混在一起了。 secondary_axis
的函数及其反函数旨在转换坐标,而不是标签。
原则上使用辅助轴会很好,如果它允许设置不同的格式化程序。不幸的是,目前情况并非如此。
另一种方法是使用双轴。但这不适用于具有相同纵横比的轴。
因此,作为最后的手段,可以在旧轴之上创建一个新轴并对其进行一致的格式化:
import numpy as np
import matplotlib.pyplot as plt
vegetables = ["cucumber", "tomato", "lettuce", "asparagus",
"potato", "wheat", "barley"]
farmers = ["Farmer Joe", "Upland Bros.", "Smith Gardening",
"Agrifun", "Organiculture", "BioGoods Ltd.", "Cornylee Corp."]
farmers_to_vegetables = dict(zip(farmers, vegetables))
harvest = np.array([[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0],
[2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0],
[1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0],
[0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0],
[0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0],
[1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1],
[0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3]])
fig, ax = plt.subplots()
im = ax.imshow(harvest)
# We want to show all xticks...
ax.set_xticks(np.arange(len(farmers)))
# ... and label them with the respective list entries
ax.set_xticklabels(farmers)
# Rotate the tick labels and set their alignment.
plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
rotation_mode="anchor")
ax2 = fig.add_subplot(111, label="secondary")
ax2.set_aspect("equal")
ax2.set_xlim(ax.get_xlim())
ax2.set_ylim(ax.get_ylim())
ax2.tick_params(left=False, labelleft=False, bottom=False, labelbottom=False,
top=True, labeltop=True)
ax2.set_facecolor("none")
for _, spine in ax2.spines.items():
spine.set_visible(False)
ax2.set_xticks(ax.get_xticks())
ax2.set_xticklabels([farmers_to_vegetables[x.get_text()] for x in ax.get_xticklabels()])
plt.setp(ax2.get_xticklabels(), rotation=45, ha="left",
rotation_mode="anchor")
fig.tight_layout()
plt.show()
我正在尝试创建一个图,该图在底部有一个 x 轴,带有基于文本的标签,而我希望在顶部有另一个 x 轴,它具有不同的基于文本的标签。
到目前为止我发现的最接近的东西是 'secondary_axis' (Link),但是当我尝试填写基于文本的标签时,出现 TypeError: unhashable type: numpy.ndarray
。
我根据 this 中的一些代码生成了以下示例:
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
# sphinx_gallery_thumbnail_number = 2
vegetables = ["cucumber", "tomato", "lettuce", "asparagus",
"potato", "wheat", "barley"]
farmers = ["Farmer Joe", "Upland Bros.", "Smith Gardening",
"Agrifun", "Organiculture", "BioGoods Ltd.", "Cornylee Corp."]
vegetables_to_farmers = dict(zip(vegetables, farmers))
farmers_to_vegetables = dict(zip(farmers, vegetables))
harvest = np.array([[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0],
[2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0],
[1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0],
[0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0],
[0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0],
[1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1],
[0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3]])
fig, ax = plt.subplots()
im = ax.imshow(harvest)
# We want to show all ticks...
ax.set_xticks(np.arange(len(farmers)))
ax.set_yticks(np.arange(len(vegetables)))
# ... and label them with the respective list entries
ax.set_xticklabels(farmers)
# Rotate the tick labels and set their alignment.
plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
rotation_mode="anchor")
def vegetables2farmers(x):
return vegetables_to_farmers[x]
def farmers2vegetables(x):
return farmers_to_vegetables[x]
secax = ax.secondary_xaxis('top', functions=(vegetables2farmers, farmers2vegetables))
fig.tight_layout()
plt.show()
最好将蔬菜作为顶部栏中的标签。您知道如何解决这个问题吗?
您将坐标轴刻度的概念与坐标轴标签混在一起了。 secondary_axis
的函数及其反函数旨在转换坐标,而不是标签。
原则上使用辅助轴会很好,如果它允许设置不同的格式化程序。不幸的是,目前情况并非如此。
另一种方法是使用双轴。但这不适用于具有相同纵横比的轴。
因此,作为最后的手段,可以在旧轴之上创建一个新轴并对其进行一致的格式化:
import numpy as np
import matplotlib.pyplot as plt
vegetables = ["cucumber", "tomato", "lettuce", "asparagus",
"potato", "wheat", "barley"]
farmers = ["Farmer Joe", "Upland Bros.", "Smith Gardening",
"Agrifun", "Organiculture", "BioGoods Ltd.", "Cornylee Corp."]
farmers_to_vegetables = dict(zip(farmers, vegetables))
harvest = np.array([[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0],
[2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0],
[1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0],
[0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0],
[0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0],
[1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1],
[0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3]])
fig, ax = plt.subplots()
im = ax.imshow(harvest)
# We want to show all xticks...
ax.set_xticks(np.arange(len(farmers)))
# ... and label them with the respective list entries
ax.set_xticklabels(farmers)
# Rotate the tick labels and set their alignment.
plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
rotation_mode="anchor")
ax2 = fig.add_subplot(111, label="secondary")
ax2.set_aspect("equal")
ax2.set_xlim(ax.get_xlim())
ax2.set_ylim(ax.get_ylim())
ax2.tick_params(left=False, labelleft=False, bottom=False, labelbottom=False,
top=True, labeltop=True)
ax2.set_facecolor("none")
for _, spine in ax2.spines.items():
spine.set_visible(False)
ax2.set_xticks(ax.get_xticks())
ax2.set_xticklabels([farmers_to_vegetables[x.get_text()] for x in ax.get_xticklabels()])
plt.setp(ax2.get_xticklabels(), rotation=45, ha="left",
rotation_mode="anchor")
fig.tight_layout()
plt.show()