1 基本图形
1.1 折线图
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ["SimHei"]
x = [x for x in range(0, 11)]
y1 = [2,5,3,6,4,6,9,22,13,14,14] #疫情前
y2 = [4,4,6,5,6,8,9,20,11,19,9]
y3 = [4,5,3,5,3,7,10,18,11,21,13]
labels = ['非常不需要', '1', '2', '3', '4', '5', '6', '7', '8', '9', '非常需要']
ax1 = plt.subplot(111)
ax1.plot(labels, y1, 'm--o', label='不同餐饮企业之间合作',color = '#00008B')
ax1.plot(x, y2, 'g--o', label='餐饮与其他非餐饮品牌联名',color = '#87CEFA')
ax1.plot(x, y3, 'b--o', label='餐饮与当地文化结合',color = '#98FB98')
ax1.set_title("用户对餐饮企业合作及文化融合的看法", fontsize=11)
ax1.set_ylim(0, 25)
ax1.set_ylabel('百分比(%)')
# ax1.set_xlabel('')
for xy1 in zip(x, y1): # text()会将文本放置在轴域的任意位置。 文本的一个常见用例是标注绘图的某些特征,而annotate()方法提供辅助函数,使标注变得容易
ax1.annotate("%s" % xy1[1], xy=xy1, xytext=(-5, 5), textcoords='offset points')
# "%s" % xy1[1]输出字符串!
for xy2 in zip(x, y2):
ax1.annotate("%s" % xy2[1], xy=xy2, xytext=(-5, 5), textcoords='offset points')
for xy3 in zip(x, y3):
ax1.annotate("%s" % xy3[1], xy=xy3, xytext=(-5, 5), textcoords='offset points')
ax1.legend()
# ax2 = plt.subplot(223)
# ax2.pie(y1, radius=1, wedgeprops={'width': 0.5}, labels=labels, autopct='%3.1f%%', pctdistance=0.75)
# ax2.set_title('产品A的销售额 ')
# ax3 = plt.subplot(224)
# ax3.pie(y2, radius=1, wedgeprops={'width': 0.5}, labels=labels, autopct='%3.1f%%', pctdistance=0.75)
# ax3.set_title('产品B的销售额 ')
# plt.tight_layout()
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()
# 实例9某地区全年平均气温降水量、蒸发量的关系
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["font.sans-serif"] = ["SimHei"]
plt.rcParams["axes.unicode_minus"] = False
month_x = np.arange(1, 6, 1)
# 疫情前
data_precipitation = np.array([16.67,38.89,18.52,18.52,7.41])
# 疫情后
data_evaporation = np.array([8.52,12.22,20.74,40.00,18.52])
fig, ax = plt.subplots()
bar_ev = ax.bar(month_x, data_precipitation, color='#87CEFA', tick_label=['从不前往','2','3','4','总去餐厅'],width = 0.4)
bar_pre = ax.bar(month_x, data_evaporation, bottom=data_precipitation, color='#00008B',width=0.4)
ax.set_ylabel('百分比(%)')
ax.set_title('疫情放开前后去餐厅频率变化对比图')
# ax_right = ax.twinx()
# ax_right.set_ylabel('气温($^\circ$C)')
# 添加图例
plt.legend([bar_ev, bar_pre], ['疫情放开前', '疫情放开后'],
shadow=True, fancybox=True)
plt.show()
import matplotlib.pyplot as plt
import pandas as pd
import cmaps
import seaborn as sns
# 获取'人均GDP(元)'这一列的值
y = [16.95,22.03,23.73,37.29]
y = sorted(y)
# 获取索引列的值(年份)
# x =['18-22岁',,'25-35岁','35-45岁','45-55岁','55岁以上']
x = ['多于3000元','少于1000元','2000-3000元','1000-2000元']
# 指定默认字体(防止中文出现乱码)
from pylab import mpl
plt.rcParams["font.sans-serif"] = ["SimHei"]
plt.rcParams["axes.unicode_minus"] = False
fig, ax = plt.subplots()
new_blues = sns.color_palette("GnBu", 20)[8:16]
bar_ev = ax.bar(x, y, color=new_blues,width=0.4)
# bar_pre = ax.bar(month_x, data_evaporation, bottom=data_precipitation, color='green')
ax.set_ylabel('百分比(%)')
ax.set_title('未就业人群的月生活费水平分布图')
ax.set_ylim(0,40)
for xy1 in zip(x, y): # text()会将文本放置在轴域的任意位置。 文本的一个常见用例是标注绘图的某些特征,而annotate()方法提供辅助函数,使标注变得容易
ax.annotate("%s" % xy1[1], xy=(xy1[0],xy1[1]), xytext=(-5, 5), textcoords='offset points')
plt.show()
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["axes.unicode_minus"] = False
plt.rcParams["font.sans-serif"] = ["SimHei"]
n = 5
X = np.arange(1,n+1)
Y1 = np.array([4.44, 12.96, 8.52, 7.78, 12.59])
Y2 = np.array([-8.89,-10.37,-13.7,-8.89,-5.93])
plt.bar(X,+Y1,facecolor='#00008B',edgecolor='white',label ='到店就餐',width=0.6)# +:向上
plt.bar(X,Y2,facecolor='#87CEFA',edgecolor='white',label = '线上团购点餐',width=0.6)# -:向下
for x,y in zip(X,Y1): # zip:X和 Y1 分别赋值给x,y
plt.text(x,y,'%.2f' % y,ha='center',va='bottom') # ha:horizontal alignment 横向对齐方式,va纵向对其方式
for x,y in zip(X,Y2): # zip:X和 Y2 分别赋值给x,y
plt.text(x,y+.15,'%.2f' % -y,ha='center',va='bottom') # ha:horizontal alignment 横向对齐方式
plt.legend()
plt.xlim(0.5,n+1)
# plt.xticks(())
plt.ylim(-15,15)
# plt.yticks(())
plt.ylabel("百分比(%)")
plt.xlabel("喜爱程度")
plt.title("到店点餐和线上团购点餐对比图")
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
import cmaps
import seaborn as sns
# 获取'人均GDP(元)'这一列的值
y = [0.0704,0.3037,0.2815,0.1926,0.1111,0.0407]
y = sorted(y)
# 获取索引列的值(年份)
# x =['18-22岁',,'25-35岁','35-45岁','45-55岁','55岁以上']
x = ['55岁以上','18-22岁','45-55岁','35-45岁','25-35岁','22-25岁']
# 指定默认字体(防止中文出现乱码)
from pylab import mpl
plt.rcParams["font.sans-serif"] = ["SimHei"]
plt.rcParams["axes.unicode_minus"] = False
# sns.palplot(sns.color_palette("GnBu", 30))
#
# sns.palplot(sns.color_palette("GnBu", 10)[2:9])
new_blues = ["orange"]+sns.color_palette("GnBu", 10)[2:7]
# 饼状图
fig, ax = plt.subplots()
ax.pie(y, labels=x,colors=new_blues,autopct='%3.2lf%%',)
plt.title('问卷调查年龄分布情况')
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()
1.10 圆环进度条
import matplotlib.pyplot as plt
from math import pi
import numpy as np
from matplotlib.patches import Patch
from matplotlib.lines import Line2D
import seaborn as sns
plt.rcParams['font.sans-serif'] = ["SimHei"]
fig, ax = plt.subplots(figsize=(8, 8))
ax = plt.subplot(projection='polar')
data = [17.14,28.57,52.86,57.14,60]
startangle = 90
# sns.palplot(sns.color_palette("GnBu", 30))
#
# sns.palplot(sns.color_palette("GnBu", 10)[2:9])
new_blues = sns.color_palette("GnBu", 10)[2:9]
colors = new_blues
# colors = ['#4393E5', '#43BAE5', '#7AE6EA']
xs = [(i * pi * 2) / 100 for i in data]
ys = [-1.4,-0.2, 1, 2.2,3.4]
left = (startangle * pi * 2) / 360 # this is to control where the bar starts
# plot bars and points at the end to make them round
for i, x in enumerate(xs):
ax.barh(ys[i], x, left=left, height=1, color=colors[i])
ax.scatter(x + left, ys[i], s=350, color=colors[i], zorder=2)
ax.scatter(left, ys[i], s=350, color=colors[i], zorder=2)
plt.ylim(-4, 4)
# legend
legend_elements = [Line2D([0], [0], marker='o', color='w', label='外卖配送时间长', markerfacecolor=colors[0] , markersize=10),
Line2D([0], [0], marker='o', color='w', label='喜欢自己做饭', markerfacecolor=colors[1], markersize=10),
Line2D([0], [0], marker='o', color='w', label='外卖费用高', markerfacecolor=colors[2], markersize=10),
Line2D([0], [0], marker='o', color='w', label='担心外卖卫生质量', markerfacecolor=colors[2], markersize=10),
Line2D([0], [0], marker='o', color='w', label='喜欢餐厅就餐', markerfacecolor=colors[2], markersize=10)
]
ax.legend(handles=legend_elements, loc='center', frameon=False)
# clear ticks, grids, spines
plt.xticks([])
plt.yticks([])
ax.spines.clear()
plt.title("消费者从不订购外卖或者较少订购外卖的原因分析")
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 颜色
5.1 个性化颜色
单个颜色使用: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]
5.2 实例
5.2.1 示例1
完整代码
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)
5.2.2 示例2
# 实例9某地区全年平均气温降水量、蒸发量的关系
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
plt.rcParams["font.sans-serif"] = ["SimHei"]
plt.rcParams["axes.unicode_minus"] = False
month_x = ['关闭或转型','2','3','4','5','6','7','8','9','生意兴隆']
x = [x for x in range(0, 10)]
data_precipitation = np.array([3,2,3,6,6,17,19,13,20,10])
fig, ax = plt.subplots()
new_blues = sns.color_palette("GnBu", 20)[6:16]
bar_ev = ax.bar(month_x, data_precipitation, color=new_blues,width=0.6)
ax.set_ylabel('百分比(%)')
ax.set_title('疫情对“老字号”餐饮企业的影响图')
for xy1 in zip(x, data_precipitation): # text()会将文本放置在轴域的任意位置。 文本的一个常见用例是标注绘图的某些特征,而annotate()方法提供辅助函数,使标注变得容易
ax.annotate("%s" % xy1[1], xy=xy1, xytext=(-5, 5), textcoords='offset points')
plt.show()