Pythonpychart画图⼏种常见的形式
在讲述之前先看⼀个图的基本元素:
1 线状图:
代码:(⾮⾃⼰创作,解析来⾃⾃带例⼦)
from pychart import *
<_options()
data = [(10, 20, 30), (20, 65, 33), (1)
(30, 55, 30), (40, 45, 51),
(50, 25, 27), (60, 75, 30),
(70, 80, 42), (80, 62, 32),
(90, 42, 39), (100, 32, 39)]
xaxis = axis.X(format="/a-60/hL%d", tic_interval = 20, label="Stuff") (2)
yaxis = axis.Y(tic_interval = 20, label="Value") (3)
ar = area.T(x_axis=xaxis, y_axis=yaxis, y_range=(0,None)) (4)
plot = line_plot.T(label="foo", data=data, ycol=1, tick_mark=tick_mark.star) (5)
plot2 = line_plot.T(label="bar", data=data, ycol=2, tick_mark=tick_mark.square)
ar.add_plot(plot, plot2) (6)
ar.draw() (7)
1 对于(1),数据的第⼀个列表⽰横坐标,后⾯两列表⽰两条线对应的纵坐标;
2 (2)和(3)表⽰坐标,主要⽤来定义坐标的式样,间隔等,分别有x,y两类,xy对应的参数基本相同。处理draw_tics_above属于X⽽ draw_tics_right属于Y的属性。
label表⽰X或者Y轴的名称,label_offset表⽰显⽰的地⽅,默认为x轴的下⽅后者Y轴的左⽅,居中放置;tic_interval表⽰坐标轴的间隔,类型为数字或者函数,如果是函数的话返回值必须是能表⽰坐标
点如何话的list,tic_label_offset 表⽰坐标轴上点下的label表⽰的位置,默认为0,0,tic_len表⽰画轴上坐标的时候的长度,如果是正直,则位于坐标轴下⽅开始画,如果是上⽅,则需要⽤负值表⽰;
format表⽰坐标轴上数字或者标签的式样,具体的样式可以参见:
/pychart/doc/escape-sequence.html
3 area表⽰图形的位置和⼤⼩,
常⽤的属性:
bg_style:背景填充的样式;/pychart/doc/module-fill-style.html#module-fill-style
border_line_style: 图形框架线的样式/pychart/doc/module-line-style.html#module-line-style,你可以通过修改ar = area.T(x_axis=xaxis, y_axis=yaxis, y_range=(0,None),border_line_style=d)来看到具体的效果,(效果是在图形的区域出现红⾊框⼦)
legend:就是⽤来表⽰图形每⼀个线⽤什么表⽰类似的标⽰
loc:标⽰图形左下⾓的位置
x_axis:x轴
y_axis:Y轴
y_range:Y值得范围,如果未None的话根据给出的数据⾃动计算,⼀般使⽤MIN/MAX来计算。如(0,None)实际表⽰0和Max Y的值,为80
add_plot( plot, ...):将绘图添加到区域
draw:画图
x_grid_style:背景⽹格线的式样,读者可以使⽤进⾏验证
ar = area.T(x_axis=xaxis, y_axis=yaxis, y_range=
(0,None),border_line_style=d,x_grid_style=d_dash1,y_grid_style=d_dash2)
x_coord: Set the X coordinate system.我理解这个意思,不太知道该怎么翻译,如果有知道的可以告诉我
2 柱状图
⽰例代码:
from pychart import *
get_options()
data = [(10, 20, 30, 5), (20, 65, 33, 5),
(30, 55, 30, 5), (40, 45, 51, 7), (50, 25, 27, 3)]
chart_object.set_defaults(, size = (150, 120), y_range = (0, None),
x_coord = (data, 0))
chart_object.set_defaults(, data = data)
# Draw the 1st graph. The Y upper bound is calculated automatically.
ar = (x_axis=X(label="X label", format="/a-30{}%d"),
y_axis=Y(label="Y label", tic_interval=10))
ar.add_plot((label="foo", cluster=(0, 3)),
(label="bar", hcol=2, cluster=(1, 3)),
(label="baz", hcol=3, cluster=(2, 3)))
ar.draw()
bar_plot:
cluster:柱状在图中的排列位置。⼀般表⽰⼀个元组,第⼀个数表⽰图的相对位置,0表⽰最左边,第⼆个数表⽰这个位置上总共的bar的个数
bcol: 表⽰从base value的第⼀列抽取数据,基本上data,bcol,或者hcol决定bar
legend_fill_style、legend_line_style,line_style⽐较好理解
stack_on:值要么设置为none,要么为bar_plot,如果不为空,则⼀条位于另⼀条bar之上
3 饼状图:
from pychart import *
import sys
data = [("foo", 10),("bar", 20), ("baz", 30), ("ao", 40)]
ar = area.T(size=(150,150), legend=legend.T(),
x_grid_style = None, y_grid_style = None)
plot = pie_plot.T(data=data, arc_offsets=[0,10,0,10],
shadow = (20, -2, ay50),
label_offset = 25,
arrow_style = arrow.a3)
ar.add_plot(plot)
ar.draw()
pie_plot主要是⽤来画饼图的类。
arrow_style: 箭头的类型.
更多的箭头类型可以参考:
/pychart/doc/module-arrow.html#module-arrow
center:饼中⼼的位置
data:数据源
data_col: 数据来⾃数据源的第⼏列
label_col:标签来⾃数据源的第⼏列
start_angle:第⼀项开始画的时候的⾓度
arc_offsets:⼀般为⼀个和data长度相等的数据,分别表⽰每⼀项画图距离中⼼的位置。如果都为0,则每⼀块之间没有空隙shadow:表⽰阴影,如果设置了则在对象的下⽅设置,偏移位置为X-off, and y-off
4 矩形图:
from pychart import *
data = [ (0, 10, 30, 40, 60), (10, 20, 40, 50, 55), (20, 10, 35, 38, 43),
(30, 8, 30, 35, 39), (40, 8, 20, 28, 39) ]
ar = area.T(x_axis = axis.X(label="X axis"),
y_grid_interval = 10, y_grid_style = line_style.white,
y_axis = axis.Y(label="Y axis"),
y_grid_over_plot=1, legend = legend.T())
if theme.use_color:
colors = [ fill_style.darkseagreen, fill_style.white, fill_style.brown ]
else:
colors = [ ay90, fill_style.white, ay50 ]
ar.add_plot(range_plot.T(label="foo", data=data, fill_style = colors[0]))
ar.add_plot(range_plot.T(label="bar", data=data, min_col=2, max_col=3,
python新手代码画图fill_style = colors[1]))
ar.add_plot(range_plot.T(label="baz", data=data, min_col=3, max_col=4,
fill_style = colors[2]))
ar.draw()
range_plot 很多参数与其他的类相似,主要有两个⽐较重要的。max_col:扫描的时候较⾼的边界,默认为2。对应的都是y轴的值min_col:扫描的时候低的边界,默认为1,对应的都是y轴的值xcol:x轴的数据来源
附: Label式样
Restrictions: /h, /v, and /a must appear in the beginning of a string.
/add
Specifies the angle of the text. Parameter dd is a number between -360 to 360. Value 0 means left-to-right text, 90 means bottom-to-up, etc. If you want to print numbers right after an angle specification, put { between the angle and the number. For example, the below code shows string ""100"" at a 60-degree angle.
"/a60{}100"
/hA:
Specifies horizontal alignment of the text. A is one of "L" (left alignment), "R" (right alignment), or "C" (center alignment).
/vA:
Specifies vertical alignment of the text. A is one of "B" (bottom), "T" (top), or "M" (middle).
/T
: Switch to Times font family.
/H:
Switch to Helvetica font family.
/C:
Switch to Courier font family.
/B:
Switch to Bookman-Demi font family.
/A:
Switch to AvantGarde-Book font family.
/P:
Switch to Palatino font family.
/S:
Switch to Symbol font family.
/
F{family}:
Switch to family font family. The below example draws string "Funny" using ZapfDingbat font (which produces some meaningless output).
canvas.show(100, 200, "/F{ZapfDingbat}Funny")
The list of available fonts are the following:
Bookman-Demi Bookman-Light Courier AvantGarde-Book AvantGarde-Demi Helvetica Helvetica-Narrow Palatino NewCenturySchlbk Times Symbol ZapfChancery-MediumItalic ZapfChancery-Medium-Italic ZapfDingbats
/b:Switch to bold typeface.
/i:Switch to italic typeface.
/o:Switch to oblique typeface.
/dd: Set font size to dd points.
"/20{}2001 space odyssey!"
/cdd:
Set gray-scale to 0.dd. Gray-scale of 0 means black, 1 means white.
//, /{, /}:
Display `/', `}', or `{'.
{ ... }:
Limit the scope of escape sequences. For example, "{/10{/20Big text} and small text}" will display "Big Text" using a 20-point font, and "and small text" using a 10-point font.
$\backslash$n:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论