Python编程pygal绘图实例之XY线

时间:2021-05-22

安装pygal,可参阅:pip和pygal的安装实例教程

基本XY线:

import pygalfrom math import cos"""XY线是将各个点用直线连接起来的折线图需提供一个横纵坐标元组作为元素的列表"""xy_chart = pygal.XY()xy_chart.title = 'XY Cosinus'xy_chart.add('x = cos(y)', [(cos(x / 10.), x / 10.) for x in range(-50, 50, 5)])xy_chart.add('y = cos(x)', [(x / 10., cos(x / 10.)) for x in range(-50, 50, 5)])xy_chart.add('x = 1', [(1, -5), (1, 5)])xy_chart.add('x = -1', [(-1, -5), (-1, 5)])xy_chart.add('y = 1', [(-5, 1), (5, 1)])xy_chart.add('y = -1', [(-5, -1), (5, -1)])xy_chart.render_to_file('bar_chart.svg')

散点图:

import pygalxy_chart = pygal.XY(stroke=False)xy_chart.title = 'Correlation'xy_chart.add('A', [(0, 0), (.1, .2), (.3, .1), (.5, 1), (.8, .6), (1, 1.08), (1.3, 1.1), (2, 3.23), (2.43, 2)])xy_chart.add('B', [(.1, .15), (.12, .23), (.4, .3), (.6, .4), (.21, .21), (.5, .3), (.6, .8), (.7, .8)])xy_chart.add('C', [(.05, .01), (.13, .02), (1.5, 1.7), (1.52, 1.6), (1.8, 1.63), (1.5, 1.82), (1.7, 1.23), (2.1, 2.23), (2.3, 1.98)])xy_chart.render_to_file('bar_chart.svg')

日期:

你可以使用日期模块绘制与日期有关的图表

DateTime

import pygalfrom datetime import datetimedatetimeline = pygal.DateTimeLine( x_label_rotation=35, truncate_label=-1, x_value_formatter=lambda dt: dt.strftime('%d, %b %Y at %I:%M:%S %p'))datetimeline.add("Serie", [ (datetime(2013, 1, 2, 12, 0), 300), (datetime(2013, 1, 12, 14, 30, 45), 412), (datetime(2013, 2, 2, 6), 823), (datetime(2013, 2, 22, 9, 45), 672)])datetimeline.render_to_file('bar_chart.svg')

Date

import pygalfrom datetime import datedateline = pygal.DateLine(x_label_rotation=25)dateline.x_labels = [ date(2013, 1, 1), date(2013, 7, 1), date(2014, 1, 1), date(2014, 7, 1), date(2015, 1, 1), date(2015, 7, 1)]dateline.add("Serie", [ (date(2013, 1, 2), 213), (date(2013, 8, 2), 281), (date(2014, 12, 7), 198), (date(2015, 3, 21), 120)])dateline.render_to_file('bar_chart.svg')

Time

import pygalfrom datetime import timedateline = pygal.TimeLine(x_label_rotation=25)dateline.add("Serie", [ (time(), 0), (time(6), 5), (time(8, 30), 12), (time(11, 59, 59), 4), (time(18), 10), (time(23, 30), -1),])dateline.render_to_file('bar_chart.svg')

TimeDelta

import pygalfrom datetime import timedeltadateline = pygal.TimeDeltaLine(x_label_rotation=25)dateline.add("Serie", [ (timedelta(), 0), (timedelta(seconds=6), 5), (timedelta(minutes=11, seconds=59), 4), (timedelta(days=3, microseconds=30), 12), (timedelta(weeks=1), 10),])dateline.render_to_file('bar_chart.svg')

总结

以上就是本文关于Python编程pygal绘图实例之XY线的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章