如何在此图表上的xticks之间添加空格,以便标签不会混合在一起?

时间:2016-11-21 15:32:24

标签: python-3.x matplotlib graph

我编写了以下代码,该代码从我写的另一个文件(readscores.py)中读取,以查找来自每个州和DC的平均ACT和SAT分数。图表看起来几乎是我想要的,但正如您所看到的,x标签合并在一起并且不可读。分散这些标签的最佳方法是什么,以便它们不会挤压在一起并且仍然与各自的酒吧对齐?Here is the graph currently drawn by the graph.

import matplotlib.pyplot as plt
import readscores

bar_width = .5

with open('actsat.txt', 'r') as input_file:
    lines = input_file.readlines()
    scores = readscores.read_scores(lines)

    states = []
    avg_act = []
    avg_sat = []
    lefts = [x for x in range(1, 52)]

    for score in scores:
        states.append(score['state'])
        avg_act.append(float(score["act_average_score"]) / 36)
        avg_sat.append((int(score["sat_average_math"]) + int(score["sat_average_reading"]) + int(score["sat_average_writing"])) / -2400)


    plt.bar(left=lefts, height=avg_act, width=bar_width, bottom=0, color='lightblue', edgecolor='blue')
    plt.bar(left=lefts, height=avg_sat, width=bar_width, bottom=0, color='grey', edgecolor='black')

    plt.xlabel("State")
    plt.ylabel("Score")

    plt.xticks(range(52), [x for x in states])
    plt.yticks([-1, -0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75, 1], [2400, 1800, 1200, 600, 0, 9, 18, 27, 36])

    plt.show()

0 个答案:

没有答案