python - 在python中设置多个xlabels
问题描述
我正在用一些数据生成 5 个图。它们都应该具有相同的 ylabel,但在 x_axis 列表中给出了不同的 xlabel。例如,第一个散点图应该有 xlabel“A”等等。
我该如何管理?谢谢你
def test(x):
x_axis = ["A", "B", "C", "D", "E"]
#DATA
x = df[x_axis[x]]
y = df["Return +1M"]
y2 = df["Return +3M"]
y3 = df["Return +6M"]
colors = ['b', 'r', 'g']
#SCATTER PLOT
return_1m = plt.scatter(x,y, marker="x", color=colors[0])
return_3m = plt.scatter(x,y2, marker="x", color=colors[1])
return_6m = plt.scatter(x,y3, marker="x", color=colors[2])
plt.legend((return_1m, return_3m, return_6m),
('Return +1M', 'Return +3M', 'Return +6M'),
scatterpoints=1,
loc='lower right',
ncol=1,
fontsize=8)
plt.ylabel("Return")
plt.show()
for i in [0,1,2,3,4]:
test(i)
解决方案
fig, (ax1, ax2,ax3,ax4,ax5) = plt.subplots(1,5)
# Whatever you want your values to be:-
ax1.plot([1,2,3,4,5], [1,2,3,4,10], 'go')
ax2.plot([1,2,3,4,5], [2,3,4,5,11], 'b*')
#X labels:-
ax1.set_xlabel('whatever you want')
ax2.set_xlabel('whatever you want')
ax3.set_xlabel('whatever you want')
ax4.set_xlabel('whatever you want')
ax5.set_xlabel('whatever you want')
#You can do same with Y axis
您还可以使用循环来设置 xlabel。这样你就不需要一次又一次地写同一行
推荐阅读
- apache-spark - 使用 pyspark 限制列中的值的出现
- coq - 如何在 Coq/Agda/Idris 中以一般方式表达“Congruence”属性?
- lotus-notes - 使用 Lotusscript 在 IBM Notes 客户端中获取视图内容时未找到注释项
- c# - 将数据从视图传递到控制器时遇到问题
- python - Python:按字典键分组,然后需要执行求和操作
- python - 函数的 Python 变量参数
- c# - Datagridview - 数据源 - 参数超出范围异常
- wpf - 为什么我的绑定没有在 XAML 中更新
- macos - 如何更改 info.plist 中的 NSServices 并在系统中进行更改?
- javascript - 根据另一个对象数组过滤对象数组并返回合并数组