我正在使用 matplotlib 生成条形图。一切正常,但我不知道如何防止 x 轴的标签相互重叠。这里有一个例子:
这是 postgres 9.1 数据库的一些示例 SQL:
drop table if exists mytable;
create table mytable(id bigint, version smallint, date_from timestamp without time zone);
insert into mytable(id, version, date_from) values
('4084036', '1', '2006-12-22 22:46:35'),
('4084938', '1', '2006-12-23 16:19:13'),
('4084938', '2', '2006-12-23 16:20:23'),
('4084939', '1', '2006-12-23 16:29:14'),
('4084954', '1', '2006-12-23 16:28:28'),
('4250653', '1', '2007-02-12 21:58:53'),
('4250657', '1', '2007-03-12 21:58:53')
;
这是我的python脚本:
# -*- coding: utf-8 -*-
#!/usr/bin/python2.7
import psycopg2
import matplotlib.pyplot as plt
fig = plt.figure()
# for savefig()
import pylab
###
### Connect to database with psycopg2
###
try:
conn_string="dbname='x' user='y' host='z' password='pw'"
print "Connecting to database\n->%s" % (conn_string)
conn = psycopg2.connect(conn_string)
print "Connection to database was established succesfully"
except:
print "Connection to database failed"
###
### Execute SQL query
###
# New cursor method for sql
cur = conn.cursor()
# Execute SQL query. For more than one row use three '"'
try:
cur.execute("""
-- In which year/month have these points been created?
-- Need 'yyyymm' because I only need Months with years (values are summeed up). Without, query returns every day the db has an entry.
SELECT to_char(s.day,'yyyymm') AS month
,count(t.id)::int AS count
FROM (
SELECT generate_series(min(date_from)::date
,max(date_from)::date
,interval '1 day'
)::date AS day
FROM mytable t
) s
LEFT JOIN mytable t ON t.date_from::date = s.day
GROUP BY month
ORDER BY month;
""")
# Return the results of the query. Fetchall() = all rows, fetchone() = first row
records = cur.fetchall()
cur.close()
except:
print "Query could not be executed"
# Unzip the data from the db-query. Order is the same as db-query output
year, count = zip(*records)
###
### Plot (Barchart)
###
# Count the length of the range of the count-values, y-axis-values, position of axis-labels, legend-label
plt.bar(range(len(count)), count, align='center', label='Amount of created/edited points')
# Add database-values to the plot with an offset of 10px/10px
ax = fig.add_subplot(111)
for i,j in zip(year,count):
ax.annotate(str(j), xy=(i,j), xytext=(10,10), textcoords='offset points')
# Rotate x-labels on the x-axis
fig.autofmt_xdate()
# Label-values for x and y axis
plt.xticks(range(len(count)), (year))
# Label x and y axis
plt.xlabel('Year')
plt.ylabel('Amount of created/edited points')
# Locate legend on the plot (http://matplotlib.org/users/legend_guide.html#legend-location)
plt.legend(loc=1)
# Plot-title
plt.title("Amount of created/edited points over time")
# show plot
pylab.show()
有没有办法防止标签相互重叠?理想情况下以自动方式,因为我无法预测条的数量。