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

时间:2017-05-19 00:12:46

标签: python pandas matplotlib

我正在尝试在每个x轴点上获得一个标签。使用当前代码,标签在较长的x轴标题上被切断,并且只有每隔这么多的x轴点都有标签。

如何使图像画布更大而不切断x轴标签?

如何强制在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')

1 个答案:

答案 0 :(得分:2)

在保存图表之前添加plt.tight_layout()

此主题为matplotlib documentation