画图-matplotlib

matplotlib篇

0、

'''
显示中文
'''
plt.rcParams['font.sans-serif']=['SimHei']   #这两行用来显示汉字
plt.rcParams['axes.unicode_minus'] = False

'''
notebook 中显示
'''
%matplotlib inline

'''
在子图中显示图像
'''

fig = plt.figure()
ax1 = fig.add_subplot(221)  #这里的111 必须填写,不知道什么意思  分成1行1列的第一个图,应该是(1,1,1)这样写比较好
ax1 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)

ax1.set_ylabel('万吨')
plt.grid(True)
ax1.legend()

一、画折线图和散点图基本用法

import matplotlib.pyplot as plt
plt.scatter(x_axis,Y_test,color='b', label='')#这个可以用向量,plot不可以,要改成序列
plt.plot(x_axis,Y_test,color='b',linewidth=2,label='reality')
#下面都是设置属性
plt.xlabel("time/clock")
plt.ylabel("number of ship")
plt.xlim(xmax=24,xmin=-0.2)
plt.ylim(ymax=6,ymin=-0.1)
x_new_ticks = np.linspace(0, 23, 24)
plt.xticks(x_new_ticks)
y_new_ticks= np.linspace(0, 6, 7)
plt.yticks(y_new_ticks)
plt.title('Neural Network of Regressor of ship')
plt.text(15,5,r'mse:'% Y_test)

plt.grid(True)
plt.legend()
plt.show()

对于不同分类的数据画散点图

#这是一个2分类的数据
positive = pdData[pdData['Admitted'] == 1] # returns the subset of rows such Admitted = 1, i.e. the set of *positive* examples
negative = pdData[pdData['Admitted'] == 0] # returns the subset of rows such Admitted = 0, i.e. the set of *negative* examples

fig, ax = plt.subplots(figsize=(10,5))
ax.scatter(positive['Exam 1'], positive['Exam 2'], s=30, c='b', marker='o', label='Admitted')
ax.scatter(negative['Exam 1'], negative['Exam 2'], s=30, c='r', marker='x', label='Not Admitted')
ax.legend()
ax.set_xlabel('Exam 1 Score')
ax.set_ylabel('Exam 2 Score')

二、画柱状图

1,、bar(left, height, width=0.8, bottom=None, **kwargs)事实上,left,height,width,bottom这四个参数确定了柱体的位置和大小。


'''
bar的参数如下range(len(num_list))  :画几个柱子
    num_list :每个柱子的高度
    color='rgb':这个设置后,每个柱子颜色都不一样,但是无法确定哪个柱子是哪种颜色,通过 color 关键字参数 可以一次性设置多个颜色,例如:

plt.bar(range(len(num_list)), num_list, label='boy',fc = 'y')
    fc :设置柱状图填充的颜色
'''
# 这个是画堆叠柱状图
import matplotlib.pyplot as plt

name_list = ['Monday', 'Tuesday', 'Friday', 'Sunday']
num_list = [1.5, 0.6, 7.8, 6]
num_list1 = [1, 2, 3, 1]
plt.bar(range(len(num_list)), num_list, label='boy', fc='y')
plt.bar(range(len(num_list)), num_list1, bottom=num_list, label='girl', tick_label=name_list, fc='r')
plt.legend()
plt.show()

 

2)描边

相关的关键字参数为:

  • edgecolor 或 ec
  • linestyle 或 ls
  • linewidth 或 lw

例如:

import matplotlib.pyplot as plt

data = [5, 20, 15, 25, 10]

plt.bar(range(len(data)), data, ec='r', ls='--', lw=2)
plt.show()

3)填充

hatch 关键字可用来设置填充样式,可取值为: / , \ , | , - , + , x , o , O , . , * 。例如:

import matplotlib.pyplot as plt

data = [5, 20, 15, 25, 10]

plt.bar(range(len(data)), data, ec='k', lw=1, hatch='o')
plt.show()

3. 设置tick label 就是x轴的标签

 
  1. import matplotlib.pyplot as plt

  2. data = [5, 20, 15, 25, 10]

  3. labels = ['Tom', 'Dick', 'Harry', 'Slim', 'Jim']

  4. plt.bar(range(len(data)), data, tick_label=labels)

  5. plt.show()

条形图barh函数

import matplotlib.pyplot as plt
data = [5, 20, 15, 25, 10]
plt.barh(range(len(data)), data)
plt.show()

7. 正负条形图

import numpy as np
import matplotlib.pyplot as plt
a = np.array([5, 20, 15, 25, 10])
b = np.array([10, 15, 20, 15, 5])
plt.barh(range(len(a)), a)
plt.barh(range(len(b)), -b)
plt.show()

三、绘制饼状图

import numpy as np
import matplotlib.pyplot as plt

labels = 'A', 'B', 'C', 'D'
fracs = [15, 30.55, 44.44, 10]
explode = [0, 0.1, 0, 0]  # 0.1 凸出这部分,
plt.axes(aspect=1)  # set this , Figure is round, otherwise it is an ellipse
# autopct ,show percet
plt.pie(x=fracs, labels=labels, explode=explode, autopct='%3.1f %%',
        shadow=True, labeldistance=1.1, startangle=90, pctdistance=0.6

        )
'''
labeldistance,文本的位置离远点有多远,1.1指1.1倍半径的位置
autopct,圆里面的文本格式,%3.1f%%表示小数有三位,整数有一位的浮点数
shadow,饼是否有阴影
startangle,起始角度,0,表示从0开始逆时针转,为第一块。一般选择从90度开始比较好看
pctdistance,百分比的text离圆心的距离
patches, l_texts, p_texts,为了得到饼图的返回值,p_texts饼图内部文本的,l_texts饼图外label的文本
'''

plt.show()

猜你喜欢

转载自blog.csdn.net/weixin_42053726/article/details/86632060
今日推荐