
文章插图
6.3 折点样式
```pythonfrom matplotlib import pyplot as pltx = range(1,8) # x轴的位置y = [17, 17, 18, 15, 11, 11, 13]# 传入x和y, 通过plot画折线图plt.plot(x, y, marker='o',color = 'red',markersize='20',markeredgecolor='g',markeredgewidth = 5) plt.show()```
文章插图
**折点形状选择:**
| character | description |
| --------- | --------------------- |
| '-' | solid line style |
| '--' | dashed line style |
| '-.' | dash-dot line style |
| ':' | dotted line style |
| '.' | point marker |
| ',' | pixel marker |
| 'o' | circle marker |
| 'v' | triangle_down marker |
| '^' | triangle_up marker |
| '<' | triangle_left marker |
| '>' | triangle_right marker |
| '1' | tri_down marker |
| '2' | tri_up marker |
| '3' | tri_left marker |
| '4' | tri_right marker |
| 's' | square marker |
| 'p' | pentagon marker |
| '*' | star marker |
| 'h' | hexagon1 marker |
| 'H' | hexagon2 marker |
| '+' | plus marker |
| 'x' | x marker |
| 'D' | diamond marker |
| 'd' | thin_diamond marker |
| '|' | vline marker |
| '_' | hline marker |
6.4 设置的图片的大小和保存
```python from matplotlib import pyplot as pltimport randomx = range(2,26,2) # x轴的位置 y = [random.randint(15, 30) for i in x] # 设置图片的大小'''figsize:指定figure的宽和高,单位为英寸;dpi参数指定绘图对象的分辨率,即每英寸多少个像素,缺省值为80 ? ? ?1英寸等于2.5cm,A4纸是 21*30cm的纸张?'''# 根据画布对象plt.figure(figsize=(20,8),dpi=80)plt.plot(x,y)# 传入x和y, 通过plot画图# plt.show()# 保存(注意: 要放在绘制的下面,并且plt.show()会释放figure资源,如果在显示图像之后保存图片将只能保存空图片 。)plt.savefig('./t1.png')# 图片的格式也可以保存为svg这种矢量图格式,这种矢量图放在网页中放大后不会有锯齿# plt.savefig('./t1.svg')```6.5 绘制x轴和y轴的刻度```pythonfrom matplotlib import pyplot as pltx = range(2,26,2) # x轴的位置y = [random.randint(15, 30) for i in x]plt.figure(figsize=(20,8),dpi=80)# 设置x轴的刻度# plt.xticks(x)# plt.xticks(range(1,25))# 设置y轴的刻度# plt.yticks(y)# plt.yticks(range(min(y),max(y)+1))# 构造x轴刻度标签x_ticks_label = ["{}:00".format(i) for i in x]#rotation = 45 让字旋转45度plt.xticks(x,x_ticks_label,rotation = 45)# 设置y轴的刻度标签y_ticks_label = ["{}℃".format(i) for i in range(min(y),max(y)+1)]plt.yticks(range(min(y),max(y)+1),y_ticks_label)plt.plot(x,y)plt.show()```6.6 设置显示中文```python# matplotlib只显示英文,无法显示中文,需要修改matplotlib的默认字体# 通过matplotlib下的font_manager可以解决# 两个小时内的每分钟跳动变化from matplotlib import pyplot as pltimport matplotlibimport randomx = range(0,120)y = [random.randint(10,30) for i in range(120)]plt.figure(figsize=(20,8),dpi=80)plt.plot(x,y)# 加坐标轴信息'''另外一种写法查看linux、mac下支持的字体终端执行: fc-list查看支持的中文(冒号前面有空格) fc-list :lang=zh查看Windows下的字体:“C:WindowsFonts”可以自己下载字体文件(xxx.ttf),然后双击安装即可# my_font = font_manager.FontProperties(fname='/System/Library/Fonts/PingFang.ttc',size=18)# plt.ylabel("天气",fontproperties=my_font)'''#rotation将字体旋转45度plt.xlabel('时间',rotation=45)from matplotlib import font_managermy_font = font_manager.FontProperties(fname='/System/Library/Fonts/PingFang.ttc',size=18)plt.ylabel("次数",fontproperties=my_font)# 设置标题plt.title('每分钟跳动次数',fontproperties=my_font,color='red')plt.show()```
文章插图
6.7 一图多线
```python# 假设大家在30岁的时候,根据自己的实际情况,统计出来你和你同事各自从11岁到30岁每年交的男/女朋友的数量如列表y1和y2,请在一个图中绘制出该数据的折线图,从而分析每年交朋友的数量走势 。y1 = [1,0,1,1,2,4,3,4,4,5,6,5,4,3,3,1,1,1,1,1]y2 = [1,0,3,1,2,2,3,4,3,2,1,2,1,1,1,1,1,1,1,1]x = range(11,31)# 设置图形plt.figure(figsize=(20,8),dpi=80)plt.plot(x,y1,color='red',label='自己')plt.plot(x,y2,color='blue',label='同事')# 设置x轴刻度xtick_labels = ['{}岁'.format(i) for i in x]my_font = font_manager.FontProperties(fname='/System/Library/Fonts/PingFang.ttc',size=18)plt.xticks(x,xtick_labels,fontproperties=my_font,rotation=45)# 绘制网格(网格也是可以设置线的样式)#alpha=0.4 设置透明度plt.grid(alpha=0.4)# 添加图例(注意:只有在这里需要添加prop参数是显示中文,其他的都用fontproperties)# 设置位置loc : upper left、 lower left、 center left、 upper centerplt.legend(prop=my_font,loc='upper right')#展示plt.show()```
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 防御sqlmap攻击之动态代码防御机制
- Python数据分析之numpy最强攻略
- 天玑9000|天玑9000之王诞生!vivo X80系列跑分一骑绝尘
- 泰山旅游攻略之美食景点推荐
- 蜀汉五虎将之列 瓦岗五虎将是哪五个
- 武夷山旅游攻略之景点美食介绍
- 长勺之战的具体内容 长勺之战地点
- 张家、青铜门和西王母之间的关系 青铜门后的终极西王母长什么样
- 阴晋之战为什么不灭秦 魏晋时期以少胜多的战役
- 贺开茶山有几个寨子,茶山谱之贺开
