Matplotlib 两个 X 轴位置切换

金东均

我想做一个有两个 X 轴的图。

但我想将主机 x 轴设置为顶部,将另一个 x 轴设置为底部。

我试过了:

    axs[i].barh(bins[:len(count_vol)], count_vol, align='edge', color='black', height = 10) # horizontal bar plot
    axs[i].set_xlabel('concentration (n/m3)')
    axs[i].set_ylabel('depth (m)')
    axs[i].invert_yaxis()
    axs[i].set_title(org, y =1.0) # subplot title
    axs[i].xaxis.set_major_formatter(FormatStrFormatter('%.2f'))
    axs[i].xaxis.tick_top() # x axis to top of the subplot

    #plt.show()


    
    # add environmental data
    temp_ax = axs[i].twiny()
    temp_ax.plot(temperature, depth, color='red')
    temp_ax.set_xlabel('temperature', color='red')
    temp_ax.xaxis.tick_bottom() x axis to botton of the subplot

当我激活 'plt.show()' 时,主机 x 轴位于我想要绘制的顶部。但是在我在上面运行整个脚本之后,两个 x 轴都在底部。

我的代码有什么问题?

ynjxsjmh

有多种方法可以在顶部添加额外的 xaxis。

刻度和标签位置错误

我想temp_ax.xaxis.tick_bottom()是多余的。

import matplotlib.pyplot as plt
import numpy as np


x = np.arange(10)

fig, axs = plt.subplots(1, 3)


# secondary axis

axs[0].plot(x)
axs[0].set_xlabel('label bottom')
secax = axs[0].secondary_xaxis('top')
secax.set_xlabel('label top')

# twiny()

axs[1].set_xlabel('label bottom')
twax = axs[1].twiny()
twax.set_xlabel('label top')


# handle properties

axs[2].xaxis.tick_top()
axs[2].xaxis.set_label_position('top')
axs[2].set_xlabel('label top')

plt.show()

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

matplotlib在同一图上以日期为x轴绘制两个系列数据

在matplotlib中用两个Y轴(两个单位)绘制单个数据

如何相对移动两个几何的x轴位置

Matplotlib:创建两个分别与两个y轴对齐的子图

Matplotlib如何在两个y轴上绘制

切换两个类

两个堆叠的X轴之间的垂直线

切换阵列的两个部分的位置

Matplotlib-Twiny:如何在一个图中对齐两个X轴的值?

matplotlib-用重叠的x / y刻度在同一轴上绘制两个直方图

如何在Matplotlib中使用按钮在两个不同的图形之间切换

在tkinter中嵌入的两个matplotlib图形之间切换时,第一个图形上会出现一组额外的意外x和y轴标签和刻度线

Python-MatPlotLib-如何在我的x轴上设置两个刻度,一个在最左边,另一个在最右边?

Matplotlib:如何绘制两个具有相同x / y轴但一个沿y轴从另一个开始的条形图

共享X轴时MatPlotLib不显示两个图形

使用matplotlib连接两个不连续的x变量

matplotlib:具有两个轴的两个图表类型

用matplotlib对齐两个y轴刻度的问题

切换x和y轴数据在r中的位置

切换两个功能

如何绘制直方图的两个水平x轴标签?

带有两个 Y 轴和公共 X 轴的 Matplotlib 条形图

为两个限制设置 X 和 Y 轴

在多个 matplotlib 子图上共享两个 y 轴

Matplotlib,多面板图上的两个 x 轴标签

切换两个不同的文本位置

如何在 matplotlib.pyplot.text() 函数中为 x 和 y 轴设置两个不同的坐标系统?

在 matplotlib 上缩放两个不同的 y 轴的问题

分享两个熊猫系列图的 X 轴