查看吸烟和不吸烟者消费账单的平均值
plt.subplot(facecolor=np.random.random(size=3))
tips.groupby('smoker')["total_bill"].mean().plot(kind="bar")
plt.grid()
plt.ytick([0,10,20],["min","middle","max"],fontsize=15,color=np.random.random(size=3))
plt.xlabel('SMOKER',fontsize=25)
plt.xticks([0,1],["yes","no"],rotation=0,color=np.random.random(size=3))
plt.title("SMOKER_TOTALBILL",fontsize=40,color=np.random.random(size=3))
plt.show()
图例
tips.query('sex == "Female"')["total_bill"].plot(kind='hist', label="Female")
tips.query('sex == "Male"')["total_bill"].plot(kind="hist", label="Male")
plt.legend()
# 所有的绘图函数中,都可以使用lable设置对应图像的图例的标签
plt.plot(x, np.sin(x), label="SIN(X)")
plt.plot(x, np.cos(x), label="COS(X)")
plt.legend()
# 一个cell中展示两个画板
tips.query('sex == "Female"')["total_bill"].plot(kind='hist', label="Female")
tips.query('sex == "Male"')["total_bill"].plot(kind="hist", label="Male")
plt.legend()
plt.show()
tips.query('smoker == "Yes"')["total_bill"].plot(kind='hist', label="Smoker-Yes")
tips.query('smoker == "No"')["total_bill"].plot(kind="hist", label="Smoker-No")
plt.legend()
plt.show
# 一个画板中展示两个画布
ax1 = plt.subplot(2,1,1)
tips.query('sex == "Female"')["total_bill"].plot(kind='hist', label="Female")
tips.query('sex == "Male"')["total_bill"].plot(kind="hist", label="Male")
# plt.legend()
# plt.show()
ax2 = plt.subplot(2,1,2)
tips.query('smoker == "Yes"')["total_bill"].plot(kind='hist', label="Smoker-Yes")
tips.query('smoker == "No"')["total_bill"].plot(kind="hist", label="Smoker-No")
# plt.legend()
ax1.legend()
ax2.legend()
plt.show()
legend方法
两种传参方法:
分别在plot函数中增加label参数,再调用legend()方法显示
直接在legend方法中传入字符串列表
loc参数
loc参数用于设置图例标签的位置,一般在legend函数内
matplotlib已经预定义好几种数字表示的位置
例:
plt.plot(x, np.sin(x))
plt.legend(["Sin(x)"], loc=10)
plt.show()
# 查看每一天(男女)消费账单的分布状况
# 按照day 绘制4个子画布
# 每一个子画布,分成男女进行绘制
loc = 1
plt.figure(figsize=(20,4))
for day in tips.day.unique():
ax = plt.subplot(1,4,loc)
loc += 1
query_condition = 'day == \"{}\"'.format(day)
day_data = tips.query(query_condition)
day_data.query('sex == "Female"')["total_bill"].plot(kind="hist", label="Female")
day_data.query('sex == "Male"')["total_bill"].plot(kind="hist", label="Male")
plt.legend(loc="upper left")
plt.figure(figsize=(12,10))
loc = 1
# for i in range(4):
# ax = plt.subplot(2,2,loc)
# loc += 1
# 第一层循环,对time来分类
for time in tips.time.unique():
time_df = tips.query('time == \"{}\"'.format(time))
for sex in time_df.sex.unique():
sex_df = time_df.query('sex == \"{}\"'.format(sex))
# 设置字画布位置
ax = plt.subplot(tips.time.unique().size, time_df.sex.unique().size,loc)
loc += 1
sex_df.plot(kind='scatter', x="total_bill", y="tip", ax=ax)
plt.title("{}-{}".format(time, sex))
loc = 1
plt.figure(figsize=(10,8))
for time in tips.time.unique():
time_df = tips.query('time == \"{}\"'.format(time))
ax = plt.subplot(tips.time.unique().size,1,loc)
loc += 1
for sex in time_df.sex.unique():
sex_df = time_df.query('sex == \"{}\"'.format(sex))
sex_df.plot(kind='scatter', x="total_bill", y="tip", ax=ax, label=sex, c=np.random.random(size=3))
plt.legend(loc="upper right")
loc参数可以是2元素的元组,表示图例左下角的坐标
图例也可以超过图的界限loc = (-0.1,0.9)
[0,0] 左下
[0,1] 左上
[1,0] 右下
[1,1] 右上
plt.title("hello world")
plt.axis([-2,2,-3,3])
plt.plot(x, np.sin(x), x, np.cos(x), x, x)
plt.legend(["hello","world","normal"],loc=[0.4,1.1], ncol=2)
ncol参数
ncol控制图例中有几列,在legend中设置ncol,需要设置loc
linestyle、color、marker
修改线条样式
设置字体
mpl.rcParams[‘font.sans-serif’] = [‘SimHei’]
设置中文负号显示问题
mpl.rcParams[‘axes.unicode_minus’]=False
style_dict = {
"实线":"-",
"虚线":":",
"点划线":"-.",
"阶梯线":"steps",
"破折线":"--"
}
pad = 1.5
for k, v in style_dict.items():
pad += 1
plt.plot(x, np.sin(x+pad), color=np.random.random(size=3), linewidth=2, linestyle=v, label=k)
plt.legend(loc="upper right")
dashes=[1,3,5,7] 使用偶数个数的整数来表示自定义虚线
plt.plot(x, np.sin(x), dashes=[1,3,5,7], lw =3, color='green')
点形
facecolor 只能设置有面积的点型
plt.plot(x, np.sin(x), marker="3", markersize=20, linestyle="None",
markerfacecolor="green", markeredgecolor="red", markeredgewidth=3)
plt.plot(x, np.sin(x), color="red", alpha=0.3)
保存图片
使用figure对象的savefig的函数
filename
含有文件路径的字符串或Python的文件型对象。图像格式由文件扩展名推断得出,例如,.pdf推断出PDF,.png推断出PNG (“png”、“pdf”、“svg”、“ps”、“eps”……)
dpi
图像分辨率(每英寸点数),默认为100
facecolor
图像的背景色,默认为“w”(白色)
使用画板对象保存图像
figure = plt.figure(facecolor="red")
plt.subplot(facecolor="red")
plt.plot(x, np.sin(x), marker="*", markersize=40, markerfacecolor="yellow", ls="None", markeredgecolor="red")
plt.axis('off')
保存画板颜色需要在savefig函数内解决
figure.savefig('stars.png', facecolor="red",dpi=100)
my_白白白
发布了58 篇原创文章 · 获赞 0 · 访问量 467
私信
关注