Python pychart画图几种常见的形式

在讲述之前先看一个图的基本元素:



1 线状图:


代码:(非自己创作,解析来自自带例子)

from pychart import *

theme.get_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表示坐标轴上数字或者标签的式样,具体的样式可以参见:
http://home.gna.org/pychart/doc/escape-sequence.html

3 area表示图形的位置和大小,

常用的属性:
bg_style:背景填充的样式;http://home.gna.org/pychart/doc/module-fill-style.html#module-fill-style
border_line_style: 图形框架线的样式http://home.gna.org/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=line_style.red)来看到具体的效果,(效果是在图形的区域出现红色框子)

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=line_style.red,x_grid_style=line_style.red_dash1,y_grid_style=line_style.red_dash2)

x_coord: Set the X coordinate system.我理解这个意思,不太知道该怎么翻译,如果有知道的可以告诉我

2 柱状图



示例代码:

from pychart import *
theme.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(area.T, size = (150, 120), y_range = (0, None),
                          x_coord = category_coord.T(data, 0))
chart_object.set_defaults(bar_plot.T, data = data)

# Draw the 1st graph. The Y upper bound is calculated automatically.
ar = area.T(x_axis=axis.X(label="X label", format="/a-30{}%d"),
            y_axis=axis.Y(label="Y label", tic_interval=10))
ar.add_plot(bar_plot.T(label="foo", cluster=(0, 3)),
            bar_plot.T(label="bar", hcol=2, cluster=(1, 3)),
            bar_plot.T(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, fill_style.gray50),
                  label_offset = 25,
                  arrow_style = arrow.a3)
ar.add_plot(plot)
ar.draw()


pie_plot主要是用来画饼图的类。
arrow_style: 箭头的类型.
更多的箭头类型可以参考:
http://home.gna.org/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 = [ fill_style.gray90, fill_style.white, fill_style.gray50 ]
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,
                         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:





你可能感兴趣的:(Python)