简体   繁体   English

如何使在 for 循环中创建的子图共享相同的比例和 xticks(matplotlib)

[英]How to make that subplots created in a for loop share the same scale and xticks (matplotlib)

I have the following code snippet, to create 20 subplots, showing a line, with different markers.我有以下代码片段,用于创建 20 个子图,显示一条线,带有不同的标记。 However, in y axis don't show the same scale, and they take part of some subplots, which makes the entire figure to look ugly.但是,在 y 轴上没有显示相同的比例,并且它们占据了一些子图的一部分,这使得整个图看起来很难看。

import matplotlib.pyplot as plt
markers = [".", ",", "o","v","^","<",">","s","p","P","*","h","H","+","x","X","D","d", "|", "_"]
num_subplots = len(markers)
fig = plt.figure()
for i, j in zip(range(num_subplots), markers): 
    try: 
        i +=1
        data = np.random.random(10)
        ax = fig.add_subplot(5,5,i)
        ax.plot(data, marker = j)
    except Exception as e:
        print(e)

I found in this post some information regarding the sharex argument, which is useful, however I cannot implement this idea in a loop.我在这篇文章中发现了一些关于 sharex 参数的信息,这很有用,但是我无法在循环中实现这个想法。

How to set same scale for subplots in python using matplotlib 如何使用 matplotlib 为 python 中的子图设置相同的比例

Why don't you create the subplots first:为什么不先创建子图:

fig, axes = plt.subplots(4,5, sharex=True, sharey=True)
for marker, ax in zip(markers, axes.ravel()):
    try:
        data = np.random.random(10)
        ax.plot(data, marker=marker)
    except Exception as e:
        print(e)

Output: Output:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Matplotlib 子图的大小都相同......如何设置它们以便子图缩放包括 xticks? - Matplotlib subplots are all the same size ... how to set them up so subplots scale including the xticks? 如何使所有子图在情节中具有相同的 xticks 和 yticks? - How to make all subplots have same xticks and yticks in plotly? 将xticks添加到matplotlib中的子图中 - adding xticks to subplots in matplotlib 如何在matplotlib中操作xticks与数据框图相同 - How to manipulate xticks in matplotlib same with dataframe plot 如何从许多子图中制作相同的比例? - How can I make the same Scale from many subplots? 在循环中创建的Matplotlib子图不显示数据 - Matplotlib subplots created in loop do not display data 使用Matplotlib为子图设置commom xticks(字符串) - Set commom xticks (string) for subplots using Matplotlib 如何使用sharex和sharey结合Aspect = equal和Adjustable =&#39;box-forced&#39;在matplotlib中创建具有相同比例的子图? - How to use sharex and sharey combined with aspect=equal and adjustable='box-forced' to create subplots with the same scale in matplotlib? 如何在matplotlib中获得不同长度但比例相同的子图轴? - How can I get axis of different length but same scale for subplots in matplotlib? 使用 matplotlib 为子图设置相同的比例但不同的限制 - Setting the same scale for subplots but different limits using matplotlib
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM