如何调整 x 轴的大小并使其与 y 轴不同 Matplotlib

How to resize the x-axis and make it different from the y-axis Matplotlib

我正在使用 ElementTree、Pandas 和 Python 中的 Matplotlib 模块进行以下开发:

    def extract_name_value(signals_df):
        #print(signals_df)
        names_list = [name for name in signals_df['Name'].unique()]
        num_names_list = len(names_list)

        colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k']

        # Creation Graphic
        fig = plt.figure(figsize=(18, 20))
        plt.suptitle(f'File PXML: {rootXML}', fontsize=20, fontweight='bold', color='SteelBlue', position=(0.75, 0.90))
        fig.tight_layout()
        i = 1
        for name in names_list:
            # get data
            data = signals_df[signals_df["Name"] == name]["Value"]
            datax = signals_df["Name"]
            # x = [n for n in range(len(data))]
            x = [n for n in range(len(datax))]
            print(x)
            # get color
            j = random.randint(0, len(colors) - 1)
            # add subplots
            ax = plt.subplot(num_names_list, 1, i)
            ax.plot(x, data, drawstyle='steps', marker='o', color=colors[j], linewidth=3)
            # plt.xticks(None)
            ax.set_ylabel(name, fontsize=12, fontweight='bold', color='SteelBlue', rotation=50, labelpad=45)
            ax.grid(alpha=0.4)
            i += 1

        plt.show()

我收到以下错误:

我一直在寻找错误,我完全理解x和y的维度必须相等,但是有可能制作一个x轴大于y轴的图形吗?而且 x 轴来自与 y 轴无关的变量?这会怎样?

x 轴是它在 xml 文件的 Signal 元素中拥有的所有值的计数:I put it here because of how extensive it is 这个值比 y 轴大,但是如何考虑我从 xml 中获取的 3 个值,即信号名称、信号值作为 y 轴和信号计数作为 x 轴。非常感谢您的意见和帮助。

IIUC,您正试图根据它们在 XML 文件中的出现顺序(X 索引)绘制多个阶跃值。然后你应该根据原始数据框的 X 值进行绘图。我没有为您的代码更改太多样式等,只是修复了一点。

import xml.etree.ElementTree as ET
import pandas as pd
from matplotlib import pyplot as plt
import random

file_xml = 'example_un_child4.xml'

def transfor_data_atri(rootXML):
    file_xml = ET.parse(rootXML)
    data_XML = [
        {"Name": signal.attrib["Name"],
         "Value": int(signal.attrib["Value"].split(' ')[0])
         } for signal in file_xml.findall(".//Signal")
    ]
    
    signals_df = pd.DataFrame(data_XML)
    extract_name_value(signals_df)
    
def extract_name_value(signals_df):
    #print(signals_df)
    names_list = [name for name in signals_df['Name'].unique()]
    num_names_list = len(names_list)

    colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k']

    # Creation Graphic
    #fig = plt.figure(figsize=(18, 20), sharex=True)
    fig, ax = plt.subplots(nrows=num_names_list, figsize=(10, 15), sharex=True)
    plt.suptitle(f'File PXML: {file_xml}', fontsize=20, fontweight='bold', color='SteelBlue', position=(0.75, 0.90))
    #fig.tight_layout()
    i = 1
    for pos, name in enumerate(names_list):
        # get data
        data = signals_df[signals_df["Name"] == name]["Value"]
        datax = signals_df["Name"]
        # x = [n for n in range(len(data))]
        #x = [n for n in range(len(datax))]
        #print(x)
        # get color
        j = random.randint(0, len(colors) - 1)
        # add subplots
        #ax[pos] = plt.subplot(num_names_list, 1, i)
        ax[pos].plot(data.index, data, drawstyle='steps', marker='o', color=colors[j], linewidth=3)
        # plt.xticks(None)
        ax[pos].set_ylabel(name, fontsize=12, fontweight='bold', color='SteelBlue', rotation=50, labelpad=45)
        ax[pos].grid(alpha=0.4)
        i += 1
    fig.tight_layout()
    plt.show()

transfor_data_atri(file_xml)