简体   繁体   English

调整x轴标签并防止它们在Pandas / matplotlib中被剪掉

[英]Adjusting x-axis labels and preventing them from being cutoff in Pandas / matplotlib

I'm trying to get a label on each x-axis point. 我正在尝试在每个x轴点上获取标签。 With the current code, the labels are chopped off on the longer x-axis titles, and only every so many x axis points gets a label. 在当前代码下,标签会在较长的x轴标题上被切掉,只有每隔x轴的点都会获得标签。

How do I make the image canvas bigger to not cutoff the x-axis labels? 如何使图像画布更大而不切掉x轴标签?

How do I force each point to be labeled on the x-axis? 如何强制将每个点标记在x轴上?

import pandas as pd
import matplotlib.pyplot as plt
import sys

def plotcsv(csvpath):
    pdata = pd.read_csv(csvpath)
    pdata = pdata.set_index('Build') #Adds build column for names of x axis points

    try: # sometimes there's an extra column if there were extra commas in the csv
        pdata = pdata.drop(pdata.columns[4], axis = 1)
    except:
        pass

    p = pdata.plot(title="Power vs Build", rot=-10)
    p.set_ylabel("W")
    # print(pdata.index)
    # print(pdata['Build'])
    # plt.xticks(pdata.index, rotation=90) # Doesn't work
    plt.savefig(csvpath + '.png')

Just add plt.tight_layout() before saving the plot. 在保存图之前,只需添加plt.tight_layout()

Here's matplotlib documentation on this topic. 这是有关此主题的matplotlib文档

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM