python - 在matplotlib中,如何在图形的两边显示一个轴?

标签 python graphics matplotlib plot

我想用 matplotlib 画一个图,轴在图的两边,类似于这个图(颜色与这个问题无关):

plot

如何使用 matplotlib 执行此操作?

注意:与示例图中显示的相反,我希望两个轴完全相同,并且只想显示一个图。添加两个轴只是为了让图表更容易阅读。

最佳答案

您可以使用 tick_params() (这是我在 Jupyter 笔记本中做的):

import matplotlib.pyplot as plt

bar(range(10), range(10))
tick_params(labeltop=True, labelright=True)

生成这张图片:

Bar plot with both x and y axis labeled the same

UPD:为子图添加了一个简单示例。您应该将 tick_params() 与轴对象一起使用。

此代码设置为仅显示顶部子图的顶部标签和底部子图的底部标签(带有相应的刻度):

import matplotlib.pyplot as plt

f, axarr = plt.subplots(2)

axarr[0].bar(range(10), range(10))
axarr[0].tick_params(labelbottom=False, labeltop=True, labelleft=False, labelright=False,
                     bottom=False, top=True, left=False, right=False)

axarr[1].bar(range(10), range(10, 0, -1))
axarr[1].tick_params(labelbottom=True, labeltop=False, labelleft=False, labelright=False,
                     bottom=True, top=False, left=False, right=False)

看起来像这样:

Subplots ticks config example

关于python - 在matplotlib中,如何在图形的两边显示一个轴?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6998697/

相关文章:

Java:单击 JButton 后图形消失

python - 如何根据从文件读取的数据 z=f(x,y) 在 matplotlib 中制作 3D 绘图

python - 寻找物体中心: showing wrong coordinate outside of the target object

python - 在 python 中使用正则表达式返回唯一匹配项

javascript - 使用HTML5 canvas的所有浏览器中的无效混合结果

python - 在不改变原始轴大小的情况下制作分隔线?

python - 使用 matplotlib 绘制纯窦的频谱图

python - tflearn to_categorical : Processing data from pandas. df.values:数组数组

python - 按列表选择列(列是列表的子集)

algorithm - 使用变换矩阵围绕任意点旋转