python之基础可视化

import matplotlib.pyplot as plt
plt.plot([1,2,5],[4,5,6],color='g',linestyle='dashed')
plt.axis([0,6,2,8])   ### axis([xmin, xmax, ymin, ymax])指定x,y的坐标范围
plt.show()

一、直线图--plot

### plot直线图
import matplotlib.pyplot as plt
x = [1,2,3]
y = [5,7,4]
x2 = [1,2,3]
y2 = [10,14,12]
plt.plot(x,y,label='first line')
plt.plot(x2,y2,label='second line')
plt.xlabel('index')
plt.ylabel('values')
plt.title('plot graph')
plt.legend(loc=1) 
       ###    ‘best’           0
       ###     'upper right'     1
       ###     'upper left'      2
       ###     'lower left'      3
       ###     'lower right'     4
       ###     'right'           5
       ###     'center left'     6
       ###     'center right'    7
       ###     'lower center'    8
       ###     'upper center'    9
       ###     'center'          10
plt.show()

二、条形图--bar

### bar条形图
import matplotlib.pyplot as plt
plt.bar([1,3,5,7,9],[5,2,7,8,2], label="Example one",color='r')
plt.bar([2,4,6,8,10],[8,6,2,5,6], label="Example two", color='g')
plt.xlabel('bar number')
plt.ylabel('bar height')
plt.title('bar graph')
plt.legend()
plt.show()

三、直方图--hist

### hist直方图
import matplotlib.pyplot as plt

population_ages = [1,6,7,11,12,13,16,17,18,19,20]
bins = [0,5,10,15,20]
plt.hist(population_ages, bins,label='hist graph',histtype='bar', rwidth=0.4)
plt.xlabel('x')
plt.ylabel('y')
plt.title('hist graph')
plt.legend()
plt.show()

四、散点图--scatter

### scatter 散点图
import matplotlib.pyplot as plt
x = [1,3,4,7,8,9,10,4,8,10,11,6]
y = [5,2,4,2,1,4,5,2,8,9,10,6]
plt.scatter(x,y, label='scatter graph',color='b', s=25, marker="+")
plt.xlabel('x')
plt.ylabel('y')
plt.title('scatter graph')
plt.legend(loc=1)
plt.show()

五、堆叠图--stack

### stack 堆叠图
import matplotlib.pyplot as plt

days = [1,2,3,4,5]

sleeping = [7,8,6,11,7]
eating =   [2,3,4,3,2]
working =  [7,8,7,2,2]
playing =  [8,5,7,8,13]
plt.stackplot(days, sleeping,eating,working,playing,colors=['b','r','g','k'])
plt.plot([],[],color='b', label='Sleeping', linewidth=5)
plt.plot([],[],color='r', label='Eating', linewidth=5)
plt.plot([],[],color='g', label='Working', linewidth=5)
plt.plot([],[],color='k', label='Playing', linewidth=5)
plt.xlabel('days')
plt.ylabel('sleeping...')
plt.title('stack graph')
plt.legend()
plt.show()

六、饼状图--pie

import matplotlib.pyplot as plt

slices = [100,50,100,30]
activities = ['python','php','java','c++']
cols = ['g','r','y','b']
plt.pie(slices,labels=activities,colors=cols,autopct='%1.1f%%',explode=(0.1,0,0,0)) 
###计算出百分比,可以单独拉出某一个切片(python)
plt.title('pie graph')
plt.show()

 七、气泡图--scatter

import matplotlib.pyplot as plt
import numpy as np
production = [1125, 1725, 2250, 2875, 2900, 3750, 4125]
tem = [6, 8, 10, 13, 14, 16, 21]
rain = [25, 40, 58, 68, 110, 98, 120]
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS'] 
colors = np.random.rand(len(tem))  # 颜色数组
size = production
plt.scatter(tem, rain, s=size, c=colors, alpha=0.6,edgecolors='face',marker='o')  # 画散点图, alpha=1.8 表示不透明度为1.8
plt.ylim([0, 150])  # 纵坐标轴范围
plt.xlim([0, 30])   # 横坐标轴范围
plt.xlabel('温度')  # 横坐标轴标题
plt.ylabel('降雨量')  # 纵坐标轴标题
plt.show()

 八、箱线图--box

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS'] 
data = np.random.randn(25, 4)
df = pd.DataFrame(data, columns=list('ABCD'))
print(df)
color = {'boxes': 'DarkGreen', # 箱体颜色
         'whiskers': 'DarkOrange', # 连线颜色
         'medians': 'DarkBlue', # 中位数颜色
         'caps': 'Gray'} # 极值颜色
df.plot.box(title='箱线图',color=color, sym='r') #异常值颜色

import numpy as np
import pandas as pd
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS'] 
import matplotlib.pyplot as plt
 
data= {'city': ['西安','西安','西安','西安','西安','郑州','郑州','郑州','郑州','郑州','武汉','武汉','武汉','武汉','武汉','武汉','武汉'],
        'GDP': [1000,1200,1300,1400,1500,1600,1700,1800,1900,2500,1800,1900,2000,2300,2600,2500,2400]
        }
df= pd.DataFrame(data)
print(df)
df.boxplot(column=["GDP"], by="city",sym='r') 
plt.grid(linestyle="--", alpha=0.3)
plt.savefig('city.jpg',dpi=1000)
plt.show()
     city  GDP
0    西安  1000
1    西安  1200
2    西安  1300
3    西安  1400
4    西安  1500
5    郑州  1600
6    郑州  1700
7    郑州  1800
8    郑州  1900
9    郑州  2500
10   武汉  1800
11   武汉  1900
12   武汉  2000
13   武汉  2300
14   武汉  2600
15   武汉  2500
16   武汉  2400
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值