1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python画三维折线图_使用Python的matplotlib画折线图 柱状图 三维图

python画三维折线图_使用Python的matplotlib画折线图 柱状图 三维图

时间:2019-09-10 22:38:02

相关推荐

python画三维折线图_使用Python的matplotlib画折线图 柱状图 三维图

因为在各种场景下需要各种实验数据的对比图像,有的中还要求dpi,这些在Python中的matplotlib中都可以实现,下面是总结的各种画图命令。

打包文件:/download/pcb931126/10864654

"""

#Python中matplotlib中画图工具

#姓名:pcb

#时间:.12.20

"""

#引入画图所需要的库文件

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

from matplotlib.ticker import MultipleLocator

from mpl_toolkits.mplot3d import Axes3D

"""

画折现图

"""

# input_values=[1,2,3,4,5]

# squares=[1,4,9,16,25]

#

# plt.plot(input_values,squares,linewidth=5) #设置线宽

# plt.title("Square Number",fontsize=24) #设置图题

# plt.xlabel("Value",fontsize=14) #设置x轴的标签以及标签的大小

# plt.ylabel("Square of value",fontsize=14) #设置y轴的标签以及标签的大小

# plt.tick_params(axis="both",labelsize=14) #设置刻度标记的大小

#

# plt.show()

"""

绘制散点图

"""

# plt.scatter(2,4,s=200)

# plt.title("Square Number",fontsize=24) #设置图题

# plt.xlabel("Value",fontsize=14) #设置x轴的标签以及标签的大小

# plt.ylabel("Square of value",fontsize=14) #设置y轴的标签以及标签的大小

# plt.tick_params(axis="both",which="major",labelsize=14) #设置刻度标记的大小

# plt.show()

"""

创建窗口子图

设置子图基本元素

"""

# x=np.arange(-5,5,0.1)

# y=x*3

# fig=plt.figure(num=1,figsize=(15,8),dpi=80) #开启一个窗口,同时设置大小分辨率,参数:窗口个数,窗口大小,分辨率

# ax1=fig.add_subplot(2,1,1) #使用fig添加子图,参数:行数、列数、第几个

# ax2=fig.add_subplot(2,1,2) #使用fig添加子图,参数:行数、列数、第几个

#

# #####设置子图窗口

# ax1.set_title("python_drawing",fontsize=12) #设置图题以及字体大小

# ax1.set_xlabel("x_name",fontsize=12) #设置x轴的字体大小

# ax1.set_ylabel("y_name",fontsize=12) #设置y轴的字体大小

# plt.axis([-6,6,-10,10]) #设置横纵坐标范围,这个子图中被分为一下两个函数

# ax1.set_xlim(-5,5) #设置横轴范围,单独给图1设置x轴的范围

# ax1.set_ylim(-10,10) #设置纵轴范围,单独给图1设置y轴的范围

# xmajorLocator = MultipleLocator(2) #定义横向主刻度标签的刻度差为2的倍数。就是隔几个刻度才显示一个标签文本

# ymajorLocator = MultipleLocator(3) #定义纵向主刻度标签的刻度差为3的倍数。就是隔几个刻度才显示一个标签文本

# ax1.xaxis.set_major_locator(xmajorLocator) #应用定义的横向主刻度格式

# ax1.yaxis.set_major_locator(ymajorLocator) #应用定义的纵向主刻度格式

# ax1.xaxis.grid(True,which="major") #x坐标轴网格使用主刻度格式

# ax1.yaxis.grid(True,which="major") #y坐标轴网格使用主刻度格式

# ax1.set_xticks([]) #去除坐标刻度

# ax1.set_xticks((-5,-3,-1,1,3,5)) #设置坐标刻度

#

# #设置刻度的显示文本,rotation旋转角度(>0顺时针旋转、<0逆时针旋转),fontsize字体大小

# #ax1.set_xticklabels(labels=['x1','x2','x3','x4','x5'],rotation=-30,fontsize='small')

#

# #标志marker为设置线的格式,具体标志如下所示:

# """

# o’ 圆圈

# ‘.’ 点

# ‘D’ 菱形

# ‘s’ 正方形

# ‘h’ 六边形1

# ‘*’ 星号

# ‘H’ 六边形2

# ‘d’ 小菱形

# ‘_’ 水平线

# ‘v’ 一角朝下的三角形

# ‘8’ 八边形

# ‘

# ‘p’ 五边形

# ‘>’ 一角朝右的三角形

# ‘,’ 像素

# ‘^’ 一角朝上的三角形

# ‘+’ 加号

# ‘\ ‘ 竖线

# ‘None’,’’,’ ‘ 无

# ‘x’ X

#

# """

# #标志color为颜色,具体标志如下所示

# """

# b 蓝色

# g 绿色

# r 红色

# y 黄色

# c 青色

# k 黑色

# m 洋红色

# w 白色

#

# """

#

# plot1=ax1.plot(x,y,marker='o',color='g',label='legend1')

#

#

# # #线图:linestyle线性,alpha透明度,color颜色,label图例文本

# plot2=ax1.plot(x,y,linestyle='--',alpha=0.5,color='r',label='legend2')

#

# ax1.legend(loc='upper left') #显示图例,plt.legend()

# ax1.text(2.8, 7, r'y=3*x') #指定位置显示文字,plt.text()

# #

# # #添加标注,参数:注释文本、指向点、文字位置、箭头属性

# ax1.annotate('important point', xy=(2, 6), xytext=(3, 1.5), arrowprops=dict(facecolor='black', shrink=0.05))

# #

# #显示网格。which参数的值为major(只绘制大刻度)、minor(只绘制小刻度)、both,默认值为major。axis为'x','y','both'

# ax1.grid(b=True,which='major',axis='both',alpha= 0.5,color='skyblue',linestyle='--',linewidth=2)

#

# # # 在当前窗口添加一个子图,rect=[左, 下, 宽, 高],是使用的绝对布局,不和以存在窗口挤占空间

# axes1 = plt.axes([.2, .3, .1, .1], facecolor='y')

# #

# # # 在子图上画图

# axes1.plot(x,y)

#

# #savefig保存图片,dpi分辨率,bbox_inches子图周边白色空间的大小

# plt.savefig('aa.png',dpi=400,bbox_inches='tight')

#

# #打开窗口,对于方法1创建在窗口一定绘制,对于方法2方法3创建的窗口,若坐标系全部空白,则不绘制

# plt.show()

"""

极坐标图

"""

# fig=plt.figure(2) #新开启一个窗口

# ax1=fig.add_subplot(1,2,1,polar=True) #启动一个窗口的极坐标子图

# theta=np.arange(0,2*np.pi,0.02) #角度数列值

# ax1.plot(theta,2*np.ones_like(theta),lw=2) #画图,参数:角度,半径,线宽

# ax1.plot(theta,theta/6,linestyle="--",lw=2) #画图,参数:角度,半径,linestyle样式,lw线宽

#

# ax2=fig.add_subplot(1,2,2,polar=True) #启动一个极坐标子图

# ax2.plot(theta,np.cos(5*theta),linestyle="--",lw=2)

# ax2.plot(theta,2*np.cos(4*theta),lw=2)

# ax2.set_rgrids(np.arange(0.2,0.2,0.2),angle=45) #距离网格轴,轴线刻度和显示位置

# ax2.set_thetagrids([0,45,90]) #角度网格轴,范围0-360度

# plt.savefig('11.png',dpi=400,bbox_inches='tight')

# plt.show()

"""

柱状图

"""

# plt.figure(3)

# x_index=np.arange(5) #柱的索引

# x_data=("A","B","C","D","E")

# y1_data=(20,35,30,35,27)

# y2_data=(25,32,34,20,25)

# bar_width=0.35 #定义一个数字代表柱的宽度

# rects1 = plt.bar(x_index, y1_data, width=bar_width,alpha=0.4, color='b',label='legend1') #参数:左偏移、高度、柱宽、透明度、颜色、图例

# rects2 = plt.bar(x_index + bar_width, y2_data, width=bar_width,alpha=0.5,color='r',label='legend2') #参数:左偏移、高度、柱宽、透明度、颜色、图例

#

# #关于左偏移,不用关心每根柱的中心不中心,因为只要把刻度线设置在柱的中间就可以了

# plt.xticks(x_index + bar_width/2, x_data) #x轴刻度线

# plt.legend() #显示图例

# plt.tight_layout() #自动控制图像外部边缘,此方法不能够很好的控制图像间的间隔

# plt.savefig('11.png',dpi=400,bbox_inches='tight')

# plt.show()

"""

直方图

"""

# fig,(ax0,ax1) = plt.subplots(nrows=2,figsize=(9,6)) #在窗口上添加2个子图

# sigma = 1 #标准差

# mean = 0 #均值

# x=mean+sigma*np.random.randn(10000) #正态分布随机数

# ax0.hist(x,bins=20,density=False,histtype='bar',facecolor='yellowgreen',alpha=0.75,rwidth=0.8) #normed是否归一化,histtype直方图类型,facecolor颜色,alpha透明度

# ax1.hist(x,bins=20,density=False,histtype='bar',facecolor='pink',alpha=0.75,cumulative=False,rwidth=0.8) #bins柱子的个数,cumulative是否计算累加分布,rwidth柱子宽度

# plt.savefig('12.png',dpi=400,bbox_inches='tight')

# plt.show() #所有窗口运行

"""

散点图

"""

# fig = plt.figure(4) #添加一个窗口

# ax =fig.add_subplot(1,1,1) #在窗口上添加一个子图

# x=np.random.random(100) #产生随机数组

# y=np.random.random(100) #产生随机数组

# ax.scatter(x,y,c='y',alpha=0.5,facecolors='none') #x横坐标,y纵坐标,s图像大小,c颜色,marker图片,lw图像边框宽度

# plt.show() #所有窗口运行

"""

三维图

"""

# fig = plt.figure(5)

# ax=fig.add_subplot(1,1,1,projection='3d') #绘制三维图

#

# x,y=np.mgrid[-2:2:20j,-2:2:20j] #获取x轴数据,y轴数据

# z=x*np.exp(-x**2-y**2) #获取z轴数据

#

# ax.plot_surface(x,y,z,rstride=2,cstride=1,cmap=plt.cm.coolwarm,alpha=0.8) #绘制三维图表面

# ax.set_xlabel('x-name') #x轴名称

# ax.set_ylabel('y-name') #y轴名称

# ax.set_zlabel('z-name') #z轴名称

# plt.savefig('12.png',dpi=400,bbox_inches='tight')

# plt.show()

"""

画矩形,多边形、圆和椭圆

"""

# fig = plt.figure(6) #创建一个窗口

# ax=fig.add_subplot(1,1,1) #添加一个子图

# rect1 = plt.Rectangle((0.1,0.2),0.2,0.3,color='r') #创建一个矩形,参数:(x,y),width,height

# circ1 = plt.Circle((0.7,0.2),0.15,color='r',alpha=0.3) #创建一个椭圆,参数:中心点,半径,默认这个圆形会跟随窗口大小进行长宽压缩

# pgon1 = plt.Polygon([[0.45,0.45],[0.65,0.6],[0.2,0.6]]) #创建一个多边形,参数:每个顶点坐标

#

# ax.add_patch(rect1) #将形状添加到子图上

# ax.add_patch(circ1) #将形状添加到子图上

# ax.add_patch(pgon1) #将形状添加到子图上

# plt.savefig('13.png',dpi=400,bbox_inches='tight')

# fig.canvas.draw() #子图绘制

# plt.show()

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

画图结果如下:

柱状图

三维图

折线图

其他图模块图

---------------------

作者:pcb931126

原文:/pcb931126/article/details/85124369

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。