1 基本图形
1.1 折线图
x = np.arange(4,19)
y_max = np.array([32,33,34,34,33,31,30,29,30,29,26,23,21,25,31])
y_min = np.array([19,19,20,22,22,21,22,16,18,18,17,14,15,16,16])
plt.title("20200806903013")
plt.plot(x,y_max)
plt.plot(x,y_min)
plt.show()
1.2 柱形图或堆积柱形图
x = np.arange(5)
y1 = np.array([10,8,7,11,13])
plt.title("20200806903013")
bar_width = 0.3
plt.bar(x,y1,tick_label=['a','b','c','d','e'],width=bar_width)
plt.show()
x = np.arange(5)
y1 = np.array([9,4,6,8,10])
y2 = np.array([10,8,7,11,13])
plt.title("20200806903013")
bar_width = 0.3
plt.bar(x,y1,tick_label=['a','b','c','d','e'],width=bar_width)
plt.bar(x+bar_width,y2,width=bar_width)
plt.show()
x = np.arange(5)
y1 = np.array([10,8,7,11,13])
y2 = np.array([9,4,6,8,10])
plt.title("20200806903013")
bar_width = 0.3
plt.bar(x,y1,tick_label=['a','b','c','d','e'],width=bar_width)
plt.bar(x,y2,bottom=y1,width=bar_width)
plt.show()
1.3 条形图
y = np.arange(5)
x1 = np.array([10,8,7,11,13])
plt.title("20200806903013")
bar_height = 0.3
plt.barh(y,x1,tick_label=['a','b','c','d','e'],height=bar_height)
plt.show()
y = np.arange(5)
x1 = np.array([10,8,7,11,13])
x2 = np.array([9,4,6,8,10])
plt.title("20200806903013")
bar_height = 0.3
plt.barh(y,x1,tick_label=['a','b','c','d','e'],height=bar_height)
plt.barh(y+bar_height,x2,height=bar_height)
plt.show()
y = np.arange(5)
x1 = np.array([10,8,7,11,13])
x2 = np.array([9,4,6,8,10])
plt.title("20200806903013")
bar_height = 0.3
plt.barh(y,x1,tick_label=['a','b','c','d','e'],height=bar_height)
plt.barh(y,x2,left=x1,height=bar_height)
plt.show()
1.3.1 当左边的label名太长没有办法完全显示时:
import matplotlib.pyplot as plt
from math import pi
import numpy as np
from matplotlib.patches import Patch
plt.rcParams['font.sans-serif'] = ["SimHei"]
y = np.arange(5)
x1 = np.array([10, 8, 7, 11, 13])
plt.title("20200806903013")
bar_height = 0.3
plt.barh(y, x1, tick_label=['订购外卖无需排队节约时间', '外卖有优惠折扣价格平易近人', '外卖平台服务态度良好', '自身烹饪水平有限', '其他'], height=bar_height)
# 设置tight bbox
plt.savefig("./11.jpg", bbox_inches='tight')
plt.show()
只需要在plt.savefig时加入参数bbox_inches='tight'即可!!!
1.3.2 条形图上显示
import matplotlib.pyplot as plt
from math import pi
import numpy as np
from matplotlib.patches import Patch
import seaborn as sns
plt.rcParams['font.sans-serif'] = ["SimHei"]
y = np.arange(5)
x1 = np.array([70, 63, 44, 29.5, 0.5])
# sns.palplot(sns.color_palette("GnBu", 30))
#
# sns.palplot(sns.color_palette("GnBu", 30)[10:1:-1])
new_blues = sns.color_palette("GnBu", 20)[10:1:-1]
colors = new_blues
bar_height = 0.5
plt.xlim(0,90)
plt.title("消费者订外卖意愿度高原因分析图")
plt.xlabel("百分比(%)")
# plt.ylabel()
plt.barh(y, x1, tick_label=['订购外卖无需排队节约时间', '外卖有优惠折扣价格平易近人', '外卖平台服务态度良好', '自身烹饪水平有限', '其他'], height=bar_height,color=colors)
# 设置tight bbox
for x, y in enumerate(x1):
plt.text(y + 0.2, x - 0.1, '%s' % y)
plt.savefig("./11.jpg", bbox_inches='tight')
plt.show()
1.4 饼图
import matplotlib.pyplot as plt
import pandas as pd
# 数据读入
df = pd.read_csv('../data/data.csv', index_col='年份')
# 获取'人均GDP(元)'这一列的值
y = df['人均GDP(元)'].values
# 获取索引列的值(年份)
x = df.index.values
# 指定默认字体(防止中文出现乱码)
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['FangSong'] # 指定‘仿宋’字体
# 饼状图
fig, ax = plt.subplots()
ax.pie(y, labels=x)
plt.show()
1.5 散点图
# 数据读入
scatter_df = pd.read_csv('../data/BankData.csv', index_col='分行编号')
x = scatter_df['各项贷款余额']
y = scatter_df['不良贷款(亿元)']
# 指定默认字体(防止中文出现乱码)
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['FangSong'] # 指定‘仿宋’字体
# 散点图
fig, ax = plt.subplots()
ax.scatter(x, y, alpha=0.5) # alpha是设置透明度,这样点重合时看的明显
# 设置标题,标签
ax.set(title='散点图', xlabel='各项贷款余额',ylabel='不良贷款(亿元)')
# 显示图形
plt.show()
1.6 堆积面积图
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(1,13)
y_a = np.array([198,215,245,222,200,236,201,253,236,200,266,290])
y_b = np.array([203,236,200,236,269,216,298,333,301,349,360,368])
y_c = np.array([185,205,226,199,238,200,250,209,246,219,253,288])
# 绘制堆积面积图
plt.stackplot(x,y_a,y_b,y_c)
plt.show()
1.7 直方图(连续的数,分组)
hist参数绘制直方图
该函数常用参数的含义如下。
x:表示x轴的数据,可以为单个数组或多个数组的序列
bins:表示矩形条的个数,默认为10
range:表示数据的范围,若没有提供range参数,则数据范围为(x.min(),x.max())
cumulative:表示是否计算累积频数或频率
histtype:表示直方图的类型,支持’bar’,‘barstacked’,‘step’,‘stepfilled’四种取值,其中’bar’为默认值,代表传统的直方图;‘barstacked’代表堆积直方图,’'step’代表未填充的线条直方图;'stepfilled’代表填充的线条直方图
align:表示矩形条边界的对齐方式,可设置为’left’,‘mid’,‘right’默认为’mid’,
orientation:表示矩形条的摆放方式,默认为’vertical’,即垂直方向
rwidth:表示矩形条宽度的百分比,默认为0.若histtype的值为’step’或’stepfilled’,则直接忽略rwidth参数的值
stacked:表示是否将多个矩形条以堆积形式摆放
import numpy as np
import matplotlib.pyplot as plt
scores = np.random.randint(0,100,50)
# 绘制直方图
plt.hist(scores,bins=8,histtype='stepfilled')
plt.show()
1.8 箱线图
import matplotlib.pyplot as plt
# 生成数据
x = [-10, -3, -2, -1, 0, 1, 2, 3, 10]
plt.boxplot(x)
plt.show()
1.9 雷达图
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.family'] = 'SimHei'
plt.rcParams['axes.unicode_minus'] = False
dim_num = 6
data = np.array([[0.40,0.32,0.35,0.30,0.30,0.88],
[0.85,0.35,0.30,0.40,0.40,0.30],
[0.43,0.59,0.30,0.28,0.22,0.30],
[0.30,0.25,0.48,0.85,0.45,0.40],
[0.20,0.38,0.87,0.45,0.32,0.28],
[0.34,0.31,0.38,0.40,0.92,0.28]])
angles = np.linspace(0,2*np.pi,dim_num,endpoint=False)
angles = np.concatenate((angles,[angles[0]]))
data = np.concatenate((data,[data[0]]))
# 维度标签
rrandar_labels = ['研究型(I)', '艺术型(A)', '社会型(S)', '企业型(E)', '传统型(C)', '现实型(R)']
radar_labels= np.concatenate((rrandar_labels,[rrandar_labels[0]]))
# 绘制雷达图
plt.polar(angles,data)
# 设置极坐标的标签
plt.thetagrids(angles*180/np.pi,labels=radar_labels)
# 填充多边形
plt.fill(angles,data,alpha=0.25)
plt.show()
2 辅助元素定制
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = np.linspace(-np.pi,np.pi,256,endpoint=True)
y1, y2 = np.sin(x),np.cos(x)
plt.plot(x,y1,x,y2)
plt.xlabel("x轴")
plt.ylabel("y轴")
plt.title('2020080603042')
plt.show()
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = np.linspace(-np.pi,np.pi,256,endpoint=True)
y1, y2 = np.sin(x),np.cos(x)
lines = plt.plot(x,y1,x,y2)
plt.axvline(x=0,linestyle='--')
plt.axhline(y=0,linestyle='--')
plt.legend(lines, ['正弦','余弦'],shadow=True,fancybox=True)
plt.plot(x,y1,x,y2)
plt.xlabel("x轴")
plt.ylabel("y轴")
plt.grid(b=True,axis='y',linewidth=1.3)
plt.xlim(x.min() *1.5, x.max() *1.5)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi], [r'$-\pi$',r'$-\pi/2$',r'$0$',r'$-\pi/2$',r'$-\pi$',])
plt.axvspan(xmin=0.5,xmax=2.0,alpha=0.3)
plt.axhspan(ymin=0.5,ymax=1.0,alpha=0.3)
plt.annotate("最小值",xy=(-np.pi / 2, -1.0),
xytext=(-(np.pi / 2), -0.5),
arrowprops=dict(arrowstyle="->"))
plt.title('2020080603042')
plt.text(3.10,0.10,"y=sin(x)",bbox=dict(alpha=0.2))
plt.table(cellText=[[6,6,6,],[8,8,8]],
colWidths=[0.1]*3,
rowLabels=['第一行','第二行'],
colLabels=['第一行','第二行','第三行'],loc='lower right')
plt.show()
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
labels=["哪吒之魔童降世","流浪地球",
"复仇者联盟4:终局之战","疯狂外星人",
"飞驰人生","烈火英雄","蜘蛛侠:英雄远征",
"速度与激情:特别行动","扫毒2:天地对决","大黄蜂",
"惊奇队长","比悲伤更悲伤的故事","哥斯拉2:怪兽之王",
"阿丽塔:战斗天使","银河补习班"]
bar_width=[48.57,46.18,42.05,21.83,16.70,14.01,13.81,
12.98,11.89,10.25,9.46,9.27,8.88,8.64,8.20]
y_data=range(len(labels))
fig=plt.figure()
ax=fig.add_subplot(111)
ax.barh(y_data,bar_width,height=0.2,color='pink')
ax.set_xlabel("总票房(亿元)")
ax.set_ylabel("电影名称")
ax.set_yticks(y_data)
ax.set_yticklabels(labels)
plt.title('2020080603042')
plt.show()
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
kinds=['购物','人情往来','餐饮美食','通信物流','生活日用','交通出行','休闲娱乐','其他']
money_scale=[800/3000,100/3000,1000/3000,300/3000,
200/3000,200/3000,200/3000,200/3000]
dev_position=[0.1,0.1,0.3,0.1,0.1,0.1,0.1,0.1]
plt.pie(money_scale,labels=kinds,autopct='%3.lf%%',
shadow=True,explode=dev_position,startangle=90)#逆时针开始绘制90度
plt.title('2020080603042')
plt.legend(kinds,loc='upper right',bbox_to_anchor=[1.3,1.1])
plt.show()
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
men_means=(90.5,89.6,88.7,88.6,86.5,84.6)
women_means=(92.8,87.0,90.5,85.4,89.6,90.2)
ind=np.arange(len(men_means))
width=0.2
fig=plt.figure()
ax=fig.add_subplot(111)
ax.bar(ind-width/2,men_means,width,label='男生平均成绩')
ax.bar(ind+0.2,women_means,width,label='女生平均成绩')
ax.set_title('高二各班男生、女生英语平均成绩')
ax.set_ylabel('分数')
ax.set_xticks(ind)
ax.set_xticklabels(['高二1班','高二2班','高三3班','高三4班','高三5班','高三6班'])
ax.axhline(88.5,ls='--',linewidth=1.0,label='全体平均成绩')
ax.legend(loc="lower right")
plt.title('2020080603042')
plt.show()
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
x=np.arange(1,8)
y=np.array([10770,16780,24440,30920,37670,48200,57270])
bar_rects=plt.bar(x,y,tick_label=["FY2013","FY2014","FY2015","FY2016","FY2017","FY2018","FY2019"],width=0.5)
def autolabel(rects):
for rect in rects:
height=rect.get_height()
plt.text(rect.get_x()+rect.get_width()/2,height+300,s='{}'.format(height),ha='center',va='bottom')
autolabel(bar_rects)
plt.ylabel('GMV(亿元)')
plt.title('2020080603042')
plt.show()
3 图标样式的美化
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["font.sans-serif"]=["SimHei"]
plt.rcParams["axes.unicode_minus"]=False
x=np.arange(5)
y1=[1200,2400,1800,2200,1600]
y2=[1050,2100,1300,1600,1340]
bar_width=0.6
tick_label=['家庭',"小说","心理","科技","儿童"]
fig=plt.figure()
ax=fig.add_subplot(111)
ax.bar(x,y1,bar_width,color="#FFCC00",align="center",label="地区1")
ax.bar(x,y2,bar_width,bottom=y1,color="#B0C4DE",align="center",label="地区2")
ax.set_ylabel("采购数量(本)")
ax.set_xlabel("图书种类")
ax.set_title("地区1和地区2对各类图书的采集情况")
ax.grid(True,axis='y',color="gray",alpha=0.2)
ax.set_xticks(x)
ax.set_xticklabels(tick_label)
ax.legend()
plt.title("2020080603042")
plt.show()
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["font.sans-serif"]=["SimHei"]
plt.rcParams["axes.unicode_minus"]=False
eurcny_2017=np.array([6.8007,6.8007,6.8015,6.8060,6.8060,
6.8060,6.8060,6.8036,6.8077,6.7877,
6.8035,6.7758,6.7700,6.7463,6.7519,
6.7511,6.7511,6.7539,6.7265])
eurcny_2019=np.array([6.8640,6.8705,6.8679,6.8679,6.8697,
6.8881,6.8853,6.8856,6.8677,6.8662,
6.8662,6.8662,6.8827,6.8761,6.8635,
6.8860,6.8737,6.8796,6.8841])
date_x=np.array([3,4,5,6,7,8,9,10,
11,12,13,14,17,18,
19,24,25,26,31])
fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot(date_x,eurcny_2017,color='#006374',linewidth=2,label='2017年7月美元/人民币汇率')
ax.plot(date_x,eurcny_2019,color='#8a2e76',linestyle='--',linewidth=2,label='2019年7月美元/人民币汇率')
ax.set_title('2017年7月与2019年7月美元/人民币汇率走势')
ax.set_xlabel('日期')
ax.set_ylabel('汇率')
ax.legend()
plt.title("2020080603042")
plt.show()
plt.plot([1,2,3],[3,4,5],marker='*',markersize=20,markerfacecolor='#FF6900')
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["font.sans-serif"]=["SimHei"]
plt.rcParams["axes.unicode_minus"]=False
sale_a=[2144,4617,7674,6666]
sale_b=[853,1214,2414,4409]
sale_c=[153,155,292,680]
fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot(sale_a,'s-',sale_b,'^:',sale_c,'s--')
ax.grid(alpha=0.3)
ax.set_ylabel('销售额(万元)')
ax.set_xticks(np.arange(len(sale_c)))
ax.set_xticklabels(['第一季度','第二季度','第三季度','第四季度'])
ax.legend(['产品A','产品B','产品C'])
plt.title("2020080603042")
plt.show()
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["font.sans-serif"]=["SimHei"]
plt.rcParams["axes.unicode_minus"]=False
x=np.arange(4,19)
y_max=[32,33,34,33,31,30,29,30,29,26,23,21,25,31,32]
y_min=[19,19,18,17,19,20,21,22,16,17,15,14,16,15,16]
plt.plot(x,y_max,marker='o',label='最高温度')
plt.plot(x,y_min,marker='o',label='最低温度')
x_temp=4
for y_h,y_1 in zip(y_max,y_min):
plt.text(x_temp-0.3,y_h+0.7,y_h,family='SimHei',fontsize=8,fontstyle='normal')
plt.text(x_temp-0.3,y_1+0.7,y_1,family='SimHei',fontsize=8,fontstyle='normal')
x_temp+=1
plt.title('未来15天最高气温和最低气温的走势2020080603042')
plt.xlabel('日期')
plt.ylabel('温度($^\circ$C)')
plt.ylim(0,40)
plt.legend()
plt.show()
import numpy as np
import matplotlib.pyplot as plt
x=np.linspace(0,8*np.pi,1000)
sin_y=np.sin(x)
cos_y=np.cos(1.5*x/np.pi)/2
plt.plot(x,sin_y)
plt.plot(x,cos_y)
plt.fill_between(x,cos_y,sin_y,cos_y<sin_y,color='#00FF11',alpha=1)
plt.fill_between(x,cos_y,sin_y,cos_y>sin_y,color='#FFE400',alpha=1)
import numpy as np
import matplotlib.pyplot as plt
def koch_snowflake(order,scale=10):
def _koch_snowflake_complex(order):
if order==0:
angles=np.array([0,120,240])+90
return scale/np.sqrt(3)*np.exp(np.deg2rad(angles)*1j)
else:
ZR=0.5-0.5j*np.sqrt(3)/3
p1 = _koch_snowflake_complex(order-1)
p2 = np.roll(p1,shift=-1)
dp = p2-p1
new_points=np.empty(len(p1)*4,dtype=np.complex128)
new_points[::4]=p1
new_points[1::4]=p1+dp/3
new_points[2::4]=p1+dp*ZR
new_points[3::4]=p1+dp/3*2
return new_points
points=_koch_snowflake_complex(order)
x,y=points.real,points.imag
return x,y
x,y=koch_snowflake(order=2)
fig=plt.figure()
ax=fig.add_subplot(111)
ax.fill(x,y,facecolor='lightsalmon',edgecolor='orangered',linewidth=3)
plt.title('2020080603042')
plt.show()
4 子图的绘制及坐标的共享
Python数据可视化(5):子图的绘制及坐标轴的共享_python子图-CSDN博客
5 颜色
单个颜色使用:https://blog.csdn.net/CD_Don/article/details/88070453
渐变颜色:
seaborn可通过设置cmap参数来选择色集绘制图表,例如我现在使用的是蓝色色集
sns.heatmap(dfData, annot=True, vmax=1, square=True, cmap="Blues")
系统自带的色集有很多!
对于颜色的三种模式,我们在使用colormap时,可以使用camp参数设置相应颜色的图,关于camp参数的的设置如下:
视觉上均匀的连续型:
连续型的:
极端型的:
离散型的:
混合型的:
自定义的话:
你这个蓝色渐变颜色最深的地方都成黑色了,能不能不要那么深,从浅蓝到深蓝就可以了
这时我们就要在原有的色集上修改
sns.palplot(sns.color_palette("Blues", 10))
首先用sns.color_palette将‘Blues’色集按照纯度将其10等分,sns.palplot()可以将色集打印出来
new_blues=sns.color_palette("Blues", 10)[0:7]
在画图的时候直接使用新的色集‘new_blues’就可以了
sns.heatmap(dfData, annot=True, vmax=1, square=True, cmap=new_blues)
如果觉得颜色不够细腻的话就将10改为1000将其1000等分后取前700
new_blues=sns.color_palette("Blues", 1000)[0:700]
完整代码
import pandas as pd
import numpy as np
%matplotlib inline
df1=pd.read_csv("abalone_data.csv")
sns.palplot(sns.color_palette("Blues", 10))
sns.palplot(sns.color_palette("Blues", 10)[0:7])
new_blues=sns.color_palette("Blues", 10)[0:7]
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(font='SimHei',font_scale=2)
def test_b(df):
dfData = df.corr()
plt.subplots(figsize=(15, 15)) # 设置画面大小
sns.heatmap(dfData, annot=True, vmax=1, square=True, cmap=new_blues)
plt.savefig('./RdBu_rStateRelation_b.png')
plt.show()
test_b(df1)