>>> import matplotlib.pyplot as mb
>>> s=[1,4,9,16,25,36]
>>> mb.plot(s)
[<matplotlib.lines.Line2D object at 0x040DACF0>]
>>> mb.show()
以上代码会绘制出一个图表,单击磁盘图标可将图形保存起来。
接下来的代码是对上面代码的改进
>>> import matplotlib.pyplot as plt
>>> s=[1,4,9,16,25,36]
>>> plt.plot(s,linewidth=5)
[<matplotlib.lines.Line2D object at 0x03FED690>]
>>> plt.title("pingfangshu",fontsize=24)
<matplotlib.text.Text object at 0x03FD5930>
>>> plt.xlabel("zhi",fontsize=24)
<matplotlib.text.Text object at 0x03FB8710>
>>> plt.ylabel("pingfangzhi",fontsize=24)
<matplotlib.text.Text object at 0x03FC26F0>
>>> plt.tick_params(axis="both",labelsize=14)
>>> plt.show()
以上代码做出来的图表更加清晰,linewidth决定了plot()绘制的线条的粗细,title()给图表指定标题,参数fontsize指定了图表中文字的大小,函数xlabel()和ylabel()让你能够为每条轴设置标题,函数tick_params()设置刻度的样式,其中指定的实参(axis="both")将影响x轴和y轴的刻度,并将刻度标记的字号设置为14(labelsize=14)
当我们向plot()提供一系列数字时,它假设第一个数据点对应的x坐标值为0,但我们的第一个点对应的x的值为1,为了改变这种默认行为,我们可以给plot()同时提供输入值和输出值,代码如下
>>> import matplotlib.pyplot as plt
>>> s=[1,4,9,16,25,36]
>>> input=[1,2,3,4,5,6]
>>> plt.plot(input,s,linewidth=5)
[<matplotlib.lines.Line2D object at 0x04325730>]
>>> plt.title("pingfangshu",fontsize=24)
<matplotlib.text.Text object at 0x0430E750>
>>> plt.xlabel("zhi",fontsize=24)
<matplotlib.text.Text object at 0x04141330>
>>> plt.ylabel("pingfangzhi",fontsize=24)
<matplotlib.text.Text object at 0x04167590>
>>> plt.tick_params(axis="both",labelsize=14)
>>> plt.show()
以下代码可以绘制出一个点
>>> import matplotlib.pyplot as plt
>>> plt.scatter(2,4)
<matplotlib.collections.PathCollection object at 0x04507590>
>>> plt.show()
以下代码是对上面代码的改进
>>> import matplotlib.pyplot as plt
>>> plt.scatter(2,4,s=200)
<matplotlib.collections.PathCollection object at 0x046E5C50>
>>> plt.title("pingfangshu",fontsize=24)
<matplotlib.text.Text object at 0x04542B50>
>>> plt.xlabel("zhi",fontsize=14)
<matplotlib.text.Text object at 0x043D4BB0>
>>> plt.ylabel("pingfangzhi",fontsize=14)
<matplotlib.text.Text object at 0x043CB730>
>>> plt.tick_params(axis="both",which="major",labelsize=14)
>>> plt.show()
以下代码是绘制多个点
>>> import matplotlib.pyplot as plt
>>> x_values=[1,2,3,4,5]
>>> y_values=[1,4,9,16,25]
>>> plt.scatter(x_values,y_values,s=100)
<matplotlib.collections.PathCollection object at 0x047614F0>
>>> plt.title("pingfangshu",fontsize=24)
<matplotlib.text.Text object at 0x0474A390>
>>> plt.xlabel("zhi",fontsize=14)
<matplotlib.text.Text object at 0x04704AF0>
>>> plt.ylabel("pingfangzhi",fontsize=14)
<matplotlib.text.Text object at 0x0452CF50>
>>> plt.tick_params(axis="both",which="major",labelsize=14)
>>> plt.show()
接下来的代码是自动绘制数据,不用手动输入数据,其中的函数axis()是用来指定每个坐标轴的取值范围的,函数axis()要求提供四个值;x和y坐标轴的最小值和最大值,在这里,我们将x坐标轴的取值范围设置为0--1100,y坐标轴的取值范围设置为0--1100000,代码如下所示
>>> import matplotlib.pyplot as plt
>>> x_values=list(range(1,1001))
>>> y_values=[x**2 for x in x_values]
>>> plt.scatter(x_values,y_values,s=40)
<matplotlib.collections.PathCollection object at 0x06F6EBD0>
>>> plt.title("pingfangzhi",fontsize=24)
<matplotlib.text.Text object at 0x06F58B50>
>>> plt.xlabel("zhi",fontsize=14)
<matplotlib.text.Text object at 0x046F0D90>
>>> plt.ylabel("pingfangzhi",fontsize=14)
<matplotlib.text.Text object at 0x06F42230>
>>> plt.tick_params(axis="both",which="major",labelsize=14)
>>> plt.axis([0,1100,0,1100000])
[0, 1100, 0, 1100000]
>>> plt.show()
接下来的代码,我们将回到pycharm中,这样容易修改,对上面代码改进了一行代码
在diyige.py文件中
#coding=utf-8
import matplotlib.pyplot as plt
x_values=list(range(1,1001))
y_values=[x**2 for x in x_values]
plt.scatter(x_values,y_values,edgecolors="none",s=40)#就修改了一下这句代码,删除数据点的轮廓,看起来效果更好
plt.title("pingfangzhi",fontsize=24)
plt.xlabel("zhi",fontsize=14)
plt.ylabel("pingfangzhi",fontsize=14)
plt.tick_params(axis="both",which="major",labelsize=14)
plt.axis([0,1100,0,1100000])
[0, 1100, 0, 1100000]
plt.show()
自定义点的颜色,代码如下所示
在diyige.py文件中
#coding=utf-8
import matplotlib.pyplot as plt
x_values=list(range(1,1001))
y_values=[x**2 for x in x_values]
plt.scatter(x_values,y_values,c="red",edgecolors="none",s=40)#就修改了一下这句代码,修改点的颜色,还可以设置参数c为元组,比如c=(0,0,0.8)
plt.title("pingfangzhi",fontsize=24)
plt.xlabel("zhi",fontsize=14)
plt.ylabel("pingfangzhi",fontsize=14)
plt.tick_params(axis="both",which="major",labelsize=14)
plt.axis([0,1100,0,1100000])
[0, 1100, 0, 1100000]
plt.show()
以下代码将点的颜色设置成了渐变色
#coding=utf-8
import matplotlib.pyplot as plt
x_values=list(range(1,1001))
y_values=[x**2 for x in x_values]
plt.scatter(x_values,y_values,c=y_values,cmap=plt.cm.Blues,edgecolors="none",s=40)#就修改了一下这句代码,将点的颜色设置为渐变色
plt.xlabel("zhi",fontsize=14)
plt.ylabel("pingfangzhi",fontsize=14)
plt.tick_params(axis="both",which="major",labelsize=14)
plt.axis([0,1100,0,1100000])
[0, 1100, 0, 1100000]
plt.show()
以下代码将图表保存为png格式的图片
#coding=utf-8
import matplotlib.pyplot as plt
x_values=list(range(1,1001))
y_values=[x**2 for x in x_values]
plt.scatter(x_values,y_values,c=y_values,cmap=plt.cm.Blues,edgecolors="none",s=40)
plt.xlabel("zhi",fontsize=14)
plt.ylabel("pingfangzhi",fontsize=14)
plt.tick_params(axis="both",which="major",labelsize=14)
plt.axis([0,1100,0,1100000])
[0, 1100, 0, 1100000]
plt.savefig("shuju.png",bbox_inches="tight")#第二个实参指定将图表多余的空白区域裁剪掉,如果要保留图表周围多余的空白区域,可忽略这个实参
以下代码将数据绘制为条形图,摇一个骰子的例子
#coding=utf-8
from random import randint
import pygal
class shaizi():
def __init__(self,sides=6):
self.sides=sides
def yao(self):
return randint(1,self.sides)
a=shaizi()
results=[]
tongji=[]
for i in range(1000):
result=a.yao()
results.append(result)
for i in range(1,a.sides+1):
ii=results.count(i)
tongji.append(ii)
print (results)
print (tongji)输出结果为[160, 187, 169, 176, 148, 160]
h=pygal.Bar()
h.title="1000 times results"
h.x_labels=["1","2","3","4","5","6"]
h.x_title="result"
h.y_title="tongji de result"
h.add('D6',tongji)
h.render_to_file("ton.svg")
以下代码将数据绘制为条形图,摇两个骰子的例子
#coding=utf-8
from random import randint
import pygal
class shaizi():
def __init__(self,sides=6):
self.sides=sides
def yao(self):
return randint(1,self.sides)
a=shaizi()
b=shaizi()
results=[]
tongji=[]
for i in range(1000):
result=a.yao()+b.yao()
results.append(result)
for i in range(2,a.sides+b.sides+1):
ii=results.count(i)
tongji.append(ii)
print (results)
print (tongji)
h=pygal.Bar()
h.title="1000 times results"
h.x_labels=["2","3","4","5","6","7","8","9","10","11","12"]
h.x_title="result"
h.y_title="tongji de result"
h.add('D12',tongji)
h.render_to_file("tq.svg")
以下代码将数据绘制为条形图,摇两个面数不同的骰子的例子,一个6面,一个10面
#coding=utf-8
from random import randint
import pygal
class shaizi():
def __init__(self,sides=6):
self.sides=sides
def yao(self):
return randint(1,self.sides)
a=shaizi()
b=shaizi(10)
results=[]
tongji=[]
for i in range(5000):
result=a.yao()+b.yao()
results.append(result)
for i in range(2,a.sides+b.sides+1):
ii=results.count(i)
tongji.append(ii)
print (results)
print (tongji)
h=pygal.Bar()
h.title="5000 times results"
h.x_labels=["2","3","4","5","6","7","8","9","10","11","12","13","14","15","16"]
h.x_title="result"
h.y_title="tongji de result"
h.add('D16',tongji)
h.render_to_file("tq.svg")