matplotlib 旋转和更改y轴标签的大小

wgmfuz8q  于 2023-06-23  发布在  其他
关注(0)|答案(1)|浏览(97)

我正在尝试做一个简单的代码行只是为了可读性的标题标签旋转。
下面是导入CSV文件的代码块:

ylabel_counter = 0
    ylabel_text = 'Sensor'
    ylabel_counter_str = ''

    df = pd.read_csv(root.filename)                      #Assume CSV file has bee selected and imported
    y = df['Total Force']
    x = df['Time Stamp']
    s_one = df['Sensor 1']
    s_two = df['Sensor 2']
    s_three = df['Sensor 3']
    s_four = df['Sensor 4']
    s_five = df['Sensor 5']
    s_six = df['Sensor 6']
    s_seven = df['Sensor 7']
    s_eight = df['Sensor 8']
    s_nine = df['Sensor 9']
    s_ten = df['Sensor 10']

    fig = plt.figure()
    gs = fig.add_gridspec(11, hspace=0.8, wspace=0)
    axs = gs.subplots(sharex=True, sharey=True)
    fig.suptitle('Forced Against Time')

    axs[0].plot(x, y)
    #axs[0].set_title('All Sensors', fontsize = 5)

    axs[1].plot(x, s_one)
    axs[2].plot(x, s_two)
    axs[3].plot(x, s_three)
    axs[4].plot(x, s_four)
    axs[5].plot(x, s_five)
    axs[6].plot(x, s_six)
    axs[7].plot(x, s_seven)
    axs[8].plot(x, s_eight)
    axs[9].plot(x, s_nine)
    axs[10].plot(x, s_ten)

    for ax in axs.flat:
        ylabel_number = ylabel_text + str(ylabel_counter)
        ax.set(xlabel='Time', ylabel=ylabel_number)                  #Area of interest, how to rotate set axes?
        ylabel_counter += 1
    
    for ax in axs:
        ax.label_outer()

    plt.show()

这是图的当前图,如ylabel所示,传感器标签都是重叠的,所以我只是试图让它们旋转90°,就像y轴刻度标签一样。先谢谢你了!

u0sqgete

u0sqgete1#

标签rotation有一个选项。只需将其设置为horizontal即可获得您想要的结果。可能需要将label命令分为两部分(x和y)。无法测试,因为我无法运行您的示例
但是像是

ylabel_number = ylabel_text + str(ylabel_counter)
ax.set_ylabel(ylabel_number, rotation='horizontal')

此外,要设置对齐方式,请尝试set_label_coords,如

ax.yaxis.set_label_coords(-0.3, 0.5)  # where coord in axis coordinate frame

参见Matplotlib示例https://matplotlib.org/stable/gallery/pyplots/align_ylabels.html

相关问题