Matplotib图形显示错误

时间:2018-02-16 10:25:33

标签: python matplotlib

在Matplotlib中绘制图形,但正如您从下图img中可以看到的那样,点(刻度)彼此距离太近。有没有办法增加刻度之间的距离而不影响任何东西,我已经搜索了很多,但没有找到任何好的,我的代码如下:

 import MySQLdb
 import sys
 import matplotlib.pyplot as plt
 from collections import Counter, OrderedDict

 # open a database connection
 connection = MySQLdb.connect()
 # prepare a cursor object using cursor() method
 cursor = connection.cursor ()

 # execute the SQL query using execute() method.
 cursor.execute ()

 #fetch all of the rows from the query
 data = cursor.fetchall()
 ddt =  sorted(set(data))

 #Shows the Count values
 coll = Counter(data)
 orddict = OrderedDict(sorted(coll.items()))# Sorts the dates in order
 y = orddict.values()
 temp = 0
 a= []
 i = 1
 j = 0
 for x in y:
   temp = temp + x
   if i % 7 == 0:
      a.insert(j,temp)
      temp = 0
      j = j + 1
   i =  i + 1
 b = 0
 dist = [b] + a
 x = range(len(y))
 plt.plot(dist ,color='green', linewidth = 2,marker='o', 
 markerfacecolor='blue', markersize=8)
 plt.ylabel('Number of Bookings')
 plt.xlabel('Date')
 plt.xticks( x,ddt[::7],fontsize = 5, rotation = 90)
 plt.show()

 #close the cursor object
 cursor.close ()

 #close the connection
 connection.close ()

 #exit the program
 sys.exit()

编辑1:按照评论中的建议进行操作,但现在图表看起来像image

1 个答案:

答案 0 :(得分:0)

你忘了按刻度标签切片。

plt.xticks( x[::7], ddt[::7], fontsize = 5, rotation = 90)

另一方面,您可能想要定义x = range(len(dist)),例如

plt.xticks( x, ddt[::7], fontsize = 5, rotation = 90)

显示所需的刻度。