自学Python——Matplotlib

0.简介

“Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形。”——百度百科

特别说明:本文包含大量图片,请在WIFI下食用
特别说明:本文包含大量图片,请在WIFI下食用
特别说明:本文包含大量图片,请在WIFI下食用
重要的事情说三遍!!!!!!!

1.初识Matplotlib

1.1 引包

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

以不同城市的高校个数为例来演示画图

school_num_city=pd.read_csv('num_city.csv',encoding='gbk')
print(school_num_city.columns)

city=school_num_city["城市代码"]
school_num=school_num_city["高校个数"]

print(school_num_city[0:3])
print(city[0:3])
print(school_num[0:3])

不同城市的高校个数

1.2折线图

1.2.1 只给y值

school_num_city_12=school_num_city[0:12]
# 只指明y轴
plt.plot(school_num_city_12['高校个数'])
plt.show()

折线图

在只有y值的情况下,x值默认从0开始,自增1

1.2.2给出x值和y值

school_num_city_12=school_num_city[0:12]
# 第一个参赛是x轴,第二个参数是y轴
plt.plot(school_num_city_12['城市代码'],school_num_city_12['高校个数'])
plt.show()

简单的折线图

1.3中文乱码问题

Matplotlib 本身并不支持中文,所以如果绘图中出现了中文,就会出现乱码
解决中文乱码问题,方法一(可以解决中文标题乱码问题)

from matplotlib.font_manager import FontProperties 
font_set = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=12)

# 指定画图区域的大小
fig=plt.figure(figsize=(10,3))

# 1.确定数据
school_num_city_12=school_num_city[0:12]
plt.plot(school_num_city_12['城市代码'],school_num_city_12['高校个数'])

# 2.对整个图进行修饰
plt.xticks(rotation=45) # 此处是将x轴的数据旋转45度
plt.xlabel(u"城市代码",fontproperties=font_set)
plt.ylabel(u"高校个数",fontproperties=font_set)
plt.title(u"城市高校分布图",fontproperties=font_set)

# 3.显示
plt.show()


2.子图绘制

2.1获取图空间

可以直接指定子图空间的尺寸

fig=plt.figure(figsize=(15,5))
# 或者直接使用fig=plt.figure()不指明画布尺寸

2.2添加子图 并确定每个图的内容

# 2.1添加子图
#  参数意义(多少行,多少列,第几个图)
ax1=fig.add_subplot(2,2,1)
ax2=fig.add_subplot(2,2,2)
ax4=fig.add_subplot(2,2,4)

# 2.2 确定每个子图的内容
ax1.plot(school_num_city[0:12]["城市代码"],school_num_city[0:12]["高校个数"])
ax2.plot(school_num_city[12:24]["城市代码"],school_num_city[12:24]["高校个数"])
ax4.plot(school_num_city[24:36]["城市代码"],school_num_city[24:36]["高校个数"])

2.3显示

plt.show()


3.同一个图中画多条折线

3.1 指定画图区域的大小

fig=plt.figure(figsize=(15,3))

3.2确定图像绘制的数据内容

参数内容(x轴数据,y轴数据,颜色,折线代表意义(图例显示的内容))

#print(help(plt.plot))
plt.plot(school_num_city[0:12]["城市代码"],school_num_city[0:12]["高校个数"] ,c='red',label="a")
# 再额外的多画几条线即可
plt.plot(school_num_city[0:12]["城市代码"],school_num_city[12:24]["高校个数"],c='blue',label="b")
plt.plot(school_num_city[0:7]["城市代码"],school_num_city[24:36]["高校个数"],c='yellow',label="c")

3.3修饰

# 2.1指明xy轴和标题
plt.xticks(rotation=45) # 此处是将x轴的数据旋转45度
plt.xlabel(u"城市代码",fontproperties=font_set)
plt.ylabel(u"高校个数",fontproperties=font_set)
plt.title(u"城市高校分布图",fontproperties=font_set)
# 2.2显示图例(让label显示)
plt.legend(loc="upper left")# loc 表示图例显示的位置, best,  upper left , upper right
#print(help(plt.legend))

3.4显示

plt.show()


4.条形图 和散点图

想要画条形图需要明确两点:
①条形图中心距离原点的距离(x),
②对应的条形图的高(y)

4.1 确定数据

# 1.确定x
bar_positions=np.arange(3)+0.5
print(bar_positions)

# 2.确定y
bar_height=school_num[0:3]
print(bar_height)

4.2画图

# fig设置画布,设置参数
# ax用来画,也可以直接用plt直接画
fig,ax=plt.subplots()
# 参数意义(x,y,宽度)
ax.bar(bar_positions,bar_height,0.2)
# 以上两条语句等于 plt.bar(bar_positions,bar_height,0.2)

# 修饰
# 设置每一个x的意义
# “意义”显示的位置
ax.set_xticks(bar_positions)
# “意义”的内容  是否旋转和其他操作
ax.set_xticklabels(['1','2','3'],rotation=45)

# 指明x轴,y轴,标题
ax.set_xlabel("x Name")
ax.set_ylabel("y Name")
ax.set_title("title ")

plt.show()

4.3 画横着的条形图

fig,ax=plt.subplots()
ax.barh(bar_positions,bar_height,0.2)
plt.show()

4.4画散点图

# 确定x y的值
node_x=np.arange(5)+2
node_y=school_num[0:5]

# 画出来
fig,ax=plt.subplots()
ax.scatter(node_x,node_y)
plt.show()


5.柱状图和盒形图

n_number=[1,2,3,5,2,3]

5.1正常画

fig,ax=plt.subplots()
ax.hist(n_number)
plt.show()

5.2设置显示多少“柱”

fig,ax=plt.subplots()
ax.hist(n_number,bins=3)
plt.show()

5.3.指定显示的区间,柱

fig,ax=plt.subplots()
# 参数意义(数据,只显示某个区间内的数据统计情况默认全部,  显示多少个柱    )
ax.hist(n_number,range=(3,5),bins=4)
ax.set_ylim(0,5)# 设置y轴区间
plt.show()

5.4盒形图

box_data=[1,9,5,8,3,7]
print(box_data)
fig,ax=plt.subplots()
ax.boxplot(box_data)
plt.show()


6.细节设置

6.1正常

temp_data=[1,9,5,8,3,7]
fig,ax=plt.subplots()
ax.plot(box_data)
plt.show()

6.2 坐标轴上的“尺度”

fig,ax=plt.subplots()
ax.plot(box_data)
ax.tick_params(bottom="off",top="off",left="off",right="off")
plt.show()

6.3设置“颜色”

temp_C=(14/255,14/255,14/255)

fig,ax=plt.subplots()
#设置颜色,设置宽度
ax.plot(box_data,c=temp_C,linewidth=10)
plt.show()

6.4给某点添加“备注”

fig,ax=plt.subplots()
ax.plot(box_data)
ax.text(1,9,"number of 9 ")
plt.show()

猜你喜欢

转载自blog.csdn.net/u011446177/article/details/79512823