以下是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()
有没有办法防止标签相互重叠?理想情况下是以自动方式,因为我无法预测棒的数量。
string
typematplotlib
将每个值打印为记号标签,记号位置是基于值数量的0索引数字。以下是如何将日期字符串转换为real datetime对象:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
data_tuples = [
('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')]
datatypes = [('col1', 'i4'), ('col2', 'i4'), ('date', 'S20')]
data = np.array(data_tuples, dtype=datatypes)
col1 = data['col1']
# convert the dates to a datetime type
dates = mdates.num2date(mdates.datestr2num(data['date']))
fig, ax1 = plt.subplots()
ax1.bar(dates, col1)
fig.autofmt_xdate()
从数据库游标中获取一个简单的元组列表应该像。。。
data_tuples = []
for row in cursor:
data_tuples.append(row)
然而,我发布了一个函数的版本,我用它来直接获取数据库游标来记录数组或熊猫数据帧:如何将SQL查询结果转换为PANDAS数据结构?
希望这也有帮助。
关于如何在xaxis上仅显示第四个勾号(例如),您可以这样做:
import matplotlib.ticker as mticker
myLocator = mticker.MultipleLocator(4)
ax.xaxis.set_major_locator(myLocator)
我认为您对matplotlib如何处理日期的几点感到困惑。
目前,你实际上并没有计划日期。您正在用[0,1,2,...]
在x轴上绘制东西,然后用日期的字符串表示手动标记每个点。
Matplotlib将自动定位刻度。然而,您正在超越matplotlib的刻度线定位功能(使用xticks
基本上是说:我希望刻度线正好位于这些位置。)
目前,如果matplotlib自动定位,您将在[10,20,30,…]
处获得刻度。但是,这些将对应于用于打印它们的值,而不是日期(打印时未使用)。
您可能希望实际使用日期绘制事物。
目前,您正在做类似的事情:
import datetime as dt
import matplotlib.dates as mdates
import numpy as np
import matplotlib.pyplot as plt
# Generate a series of dates (these are in matplotlib's internal date format)
dates = mdates.drange(dt.datetime(2010, 01, 01), dt.datetime(2012,11,01),
dt.timedelta(weeks=3))
# Create some data for the y-axis
counts = np.sin(np.linspace(0, np.pi, dates.size))
# Set up the axes and figure
fig, ax = plt.subplots()
# Make a bar plot, ignoring the date values
ax.bar(np.arange(counts.size), counts, align='center', width=1.0)
# Force matplotlib to place a tick at every bar and label them with the date
datelabels = mdates.num2date(dates) # Go back to a sequence of datetimes...
ax.set(xticks=np.arange(dates.size), xticklabels=datelabels) #Same as plt.xticks
# Make space for and rotate the x-axis tick labels
fig.autofmt_xdate()
plt.show()
相反,尝试这样的方法:
import datetime as dt
import matplotlib.dates as mdates
import numpy as np
import matplotlib.pyplot as plt
# Generate a series of dates (these are in matplotlib's internal date format)
dates = mdates.drange(dt.datetime(2010, 01, 01), dt.datetime(2012,11,01),
dt.timedelta(weeks=3))
# Create some data for the y-axis
counts = np.sin(np.linspace(0, np.pi, dates.size))
# Set up the axes and figure
fig, ax = plt.subplots()
# By default, the bars will have a width of 0.8 (days, in this case) We want
# them quite a bit wider, so we'll make them them the minimum spacing between
# the dates. (To use the exact code below, you'll need to convert your sequence
# of datetimes into matplotlib's float-based date format.
# Use "dates = mdates.date2num(dates)" to convert them.)
width = np.diff(dates).min()
# Make a bar plot. Note that I'm using "dates" directly instead of plotting
# "counts" against x-values of [0,1,2...]
ax.bar(dates, counts, align='center', width=width)
# Tell matplotlib to interpret the x-axis values as dates
ax.xaxis_date()
# Make space for and rotate the x-axis tick labels
fig.autofmt_xdate()
plt.show()
我的Python脚本里有这个 它产生了一个像这样的图表 正如你所看到的,x标签被切断了,这不是我没有完全扩展图形,我已经尝试过了,它仍然被切断。我怎样才能在图表上得到整个标签? 谢啦
问题内容: 我遇到了轴刻度标签的格式问题。我禁用了y_axis的偏移量: 并尝试将其设置为科学格式,但我得到的是: 但我期望这样的事情: 或类似。 我真正想要的是这样的: 我可以尝试将标签设置为静态,但是最后我将拥有数十或数百个具有不同值的图,因此需要动态设置。 一种替代方法是将y_axis偏移量放置为标签,但是我也不知道如何执行此操作。 问题答案: 有很多方法可以做到这一点 您可以调整功率限制(
问题内容: 是否有一种简单的方法可以将y轴标签放在图的右侧?我知道可以使用来对刻度标签进行此操作,但是我想知道是否也可以对轴标签进行此操作。 我想到的一个想法是使用 但是,在保留y轴的范围的同时,将所有标签(刻度和轴标签)放置在右侧并没有达到预期的效果。简而言之,我想要一种将所有y轴标签从左向右移动的方法。 问题答案: 看来您可以使用以下方法做到这一点: 请参阅此处的示例。
我想设置我的情节标题和x和y标签。总数都是浮点数64.我想尽可能简单地做这件事。 以下是我的代码部分:
在JFreechart中,我有一个带有日期(和时间)的X轴。 我怎样才能让JFreechart优化它们并充分利用它们? 现在它包含的标签比空间多,所有标签都转换为“…”。 如果不是所有的刻度都有标签,这是完全可以的,但是我想要尽可能多的(如果它们合适并且可以完全显示)。 我怎样才能做到这一点? 这里是完整的最小源来重现截断的标签。(默认情况下,JFreechart不处理优化: 我更喜欢像@tras
我已经想出了如何用以下代码片段(数周,放在xAxis中的代码)更改下面的工作日标签(T、W、T等): 现有示例:https://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/gantt/demo/resource-management