可视化实验三

1 条形图(课本186页:四、程序题)

# -*- coding: utf-8 -*-
import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt 
# plt.rcParams['font.sans-serif']='FangSong'
# plt.rcParams['axes.unicode_minus']=False
np.arange(0,11)
###(1)仿照图9-15的表格,创建一个DataFrame对象###
df=pd.DataFrame({'证券代码 ':['000609','000993','002615','000795','002766',
                          '000971','000633','300173','30 0279','000831'], 
                 '证券简称':['中迪投资','闽东电力','哈尔斯','英洛华','索菱股份',
                         '高升控投','合金投资','智慧松德','和晶科技','五矿稀土'], 
                 '最新价':[4.80,4.80,5.02,3.93,6.78,3.72,4.60,4.60,5.81,9.87], 
                 '涨跌幅%':[10.09,10.09,10.09,10.08,10.06,10.06,10.06,10.05,
                         10.05,10.04]})
###(2)使用条形图展示股票数据,其中证券简称为x轴、最新价为y轴###
s1=df['证券简称'] 
s2=df['最新价'] 
plt.figure(figsize=(12,6),facecolor='white') 
plt.bar(s1,align = 'center',height=s2,width=0.8,alpha=0.8) 
plt.yticks([0,1,2,3,4,5,6,7,8,9,10])  
plt.xlabel("证券简称") 
plt.ylabel("最新价") 
plt.title("各证券最新价格") 
###(3)将条形图以shares——bar.png为文件名保存在桌面上###
# plt.savefig(r'E:\pywork\shares--bar.png') 
plt.show()

2 阶梯图(例题“美国邮票价格”)

import pyecharts.options as opts
from pyecharts.charts import Line
​
c = (
    Line()
     .add_xaxis(['1995','1996','1997','1998','1999','2000'
,'2001','2002','2003','2004','2005','2006','2007','2008','2009'])
      .add_yaxis("系列1",[0.32,0.32,0.32,0.32,0.33,0.33,0.34,0.37,
0.37,0.37,0.37,0.39,0.41,0.42,0.44],is_step=True)
        .set_global_opts(
          yaxis_opts=opts.AxisOpts(
               min_=0.3,
              max_=0.45
       )
   )
)
c.render_notebook()


3 折线图(“国际原油价格走势图”,数据网上自己查)

import pyecharts.options as opts
from pyecharts.charts import Line
​
c = (
    Line()
     .add_xaxis(['8','9','10','11','12','13','14','15','16','17','18','19','20','21','22'])
      .add_yaxis("系列1",[99,99.1,99.2,99.3,99.4,99.5,99.3,99.6,99.7,99.8,99.9,100,101,100,100.6],)
        .set_global_opts(
          yaxis_opts=opts.AxisOpts(
               min_=98
       )
   )
)
c.render_notebook()


4 拟合曲线

import csv
import sys
filename = "J:\大三复习资料\数据可视化\data\data\subscribers.csv" #需要自己修改数据路径
xa = []
ya = []
with open(filename) as f:
    reader = csv.reader(f)
    for datarow in reader:
        if reader.line_num != 1:
#             print(reader.line_num,datarow)
            ya.append(float(datarow[3]))
            xa.append(int(datarow[1]))
plt.figure() #创建自定义图像
plt.scatter(xa[:], ya[:], s=10,c='g',marker='o',alpha=0.5) #绘制散点图
poly = np.polyfit(xa, ya, deg = 3) #拟合曲线
plt.plot(xa, np.polyval(poly, xa)) #多项式求值
plt.show()5  散点图(开放题目,自己设计一个课题,使用matplotlib绘制)
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
x=np.arange(51)
y=np.random.rand(51)*10
plt.scatter(x,y)
plt.show()

6 堆叠柱形图

import numpy as np
import matplotlib.pyplot as plt
 
 
N = 13
S = (52, 49, 48, 47, 44, 43, 41, 41, 40, 38, 36, 31, 29)
C = (38, 40, 45, 42, 48, 51, 53, 54, 57, 59, 57, 64, 62)
 
d=[]
for i in range(0,len(S)):
    sum = S[i] + C[i]
    d.append(sum)
M = (10, 11, 7, 11, 8, 6, 6, 5, 3, 3, 7, 5, 9)
#menStd = (2, 3, 4, 1, 2)
#womenStd = (3, 5, 2, 3, 3)
ind = np.arange(N)    # the x locations for the groups
width = 0.35       # the width of the bars: can also be len(x) sequence
 
p1 = plt.bar(ind, S, width, color='#d62728')#, yerr=menStd)
p2 = plt.bar(ind, C, width, bottom=S)#, yerr=womenStd)
p3 = plt.bar(ind, M, width, bottom=d)
 
plt.ylabel('Scores')
plt.title('Scores by group and gender')
plt.xticks(ind, ('G1', 'G2', 'G3', 'G4', 'G5','G6', 'G7', 'G8', 'G9', 'G10', 'G11', 'G12', 'G13'))
plt.yticks(np.arange(0, 81, 20))
plt.legend((p1[0], p2[0], p3[0]), ('S', 'C', 'M'))

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值