Matplotlib-x轴上等距的条/刻度

时间:2019-02-12 02:23:19

标签: python matplotlib

我正在制作条形图。在x轴上,我正在绘制与值无关的ID。我希望它们之间的间距均匀,但是现在我有代码的方式,条形的间距不均匀。我该怎么做才能使条形图/轴标签的间距均匀?我意识到这可能是重复的,但在搜索后找不到清晰的答案

import psycopg2
import matplotlib.pyplot as plt
import numpy as np



def postgres():
  conn = psycopg2.connect(host="localhost",
                        database="dvdrentals", 
                        user="postgres", 
                        password="XXXXXXXXXX")

  cursor = conn.cursor()
  sql = """
        SELECT
            payment_id,
            amount
        FROM
            public.payment
        WHERE
            (payment_id = 17503 or
            payment_id = 17504 or
            payment_id = 17508 or
            payment_id = 17511 or
            payment_id = 17514);

    """

  cursor.execute(sql)

  result = cursor.fetchall()
  print(result)

  list_x = []
  list_y = []

  for row in result:
        list_x.append(row[0])

  for row in result:
        list_y.append(row[1])


  fig = plt.figure()
  ax = fig.add_subplot(111)
  ax.bar(list_x[0], list_y[0], color = 'chartreuse')   
  ax.bar(list_x[1], list_y[1], color = 'turquoise')  
  ax.bar(list_x[2], list_y[2], color = 'olive')   
  ax.bar(list_x[3], list_y[3], color = 'darkslateblue')    
  ax.bar(list_x[4], list_y[4], color = 'crimson')  

  ax.set_xticks([17503,17504,17508,17511,17514])                                       




  plt.show()

postgres()

这是图片 http://tinypic.com/r/2uhqv6v/9

0 个答案:

没有答案