day05-matplotlit设置图形各种参数

matplotlib网格

1. 显示网格:plt.grid()

plt.grid(True, linestyle = "--",color = "gray", linewidth = "0.5",axis = 'x')
  • 显示网格
  • linestyle:线型,“–”:表示网格是虚线,默认为实线
  • color:网格颜色
  • linewidth:宽度
  • axis:x,y,both,显示x/y/两者的格网
x = np.linspace(-np.pi,np.pi,256,endpoint = True)
c, s = np.cos(x), np.sin(x)
plt.plot(x, c)
plt.plot(x, s)
# 通过ndarry创建图表
#plt.grid(True, linestyle = "--",color = "gray", linewidth = "0.5",axis = 'both')
plt.grid(True,linestyle="--")
# 显示网格
# linestyle:线型
# color:颜色
# linewidth:宽度  (lw)
# axis:x,y,both,显示x/y/两者的格网

在这里插入图片描述

2. plt.gca( ) 对坐标轴的操作

首先观察画布上面的坐标轴,如下图

在这里插入图片描述

上图中,用红色标识出的黑色边界框线在Matplotlib中被称为spines,中文翻译为脊柱…在我理解看来,意思是这些边界框线是坐标轴区域的“支柱”。

那么,我们最终要挪动的其实就是这四个“支柱”

且所有的操作均在plt.gca( )中完成,gca就是get current axes的意思

接下来需要绘制图如下:

在这里插入图片描述

  • 获取当前坐标轴位置并移动
# 创建x轴数据
x = np.arange(-50,51)
# 创建y轴的数据,他是x的平方
y = x ** 2
plt.plot(x, y)

在这里插入图片描述

x = np.arange(-50,51)
y = x ** 2
# 获取当前坐标轴
ax = plt.gca()
# 通过坐标轴spines,确定 top, bottom, left, right (分别表示上,下,左和右)

# 不需要右侧和上侧线条,则可以设置他的颜色
ax.spines['right'].set_color("none")
ax.spines['top'].set_color("none")

在这里插入图片描述

# 移动下轴到指定位置
# 在这里,position位置参数有三种,data , outward(向外-可自行尝试) , axes
# axes:0.0 - 1.0之间的值,整个轴上的比例
#ax.spines['left'].set_position(('data',0.0))
ax.spines['left'].set_position(('axes',0.5))  #0-1范围   0.5——>50%
# 移动下轴到指定位置
# 'data'表示按数值挪动,其后数字代表挪动到Y轴的刻度值
#ax.spines['bottom'].set_position(('data',0.0))

#设置坐标区间:
plt.ylim(0, y.max()) # 设置轴取值范围
plt.plot(x, y)

在这里插入图片描述

3. plt.rcParams设置画图的分辨率,大小等信息

  • plt.rcParams[‘figure.figsize’] = (8.0, 4.0) # 设置figure_size英寸
  • plt.rcParams[‘figure.dpi’] = 300 #分辨率
    • 默认的像素:[6.0,4.0],分辨率为72,图片尺寸为 432x288
    • 指定dpi=100,图片尺寸为 600*400
    • 指定dpi=300,图片尺寸为 1800*1200
plt.rcParams['figure.figsize'] = (6.0, 4.0)
plt.plot()
# 指定dpi=100,图片尺寸为 600*400
plt.rcParams['figure.dpi'] = 100
#值越大越清晰

4.图表的样式参数设置

  • (1).线条样式

    传入x,y,通过plot画图,并设置折线颜色、透明度、折线样式和折线宽度 标记点、标记点大小、标记点边颜色、标记点边宽,网格

    plt.plot(x,y,color='red',
    alpha=0.3,
    linestyle='',
    linewidth=5,
    marker='o',
    markeredgecolor='r',
    markersize='20',
    markeredgewidth=10)
    
  • 1). color:可以使用颜色的16进制,也可以使用线条颜色的英文,还可是使用之前的缩写

字符 颜色 英文全称
‘b’ 蓝色 blue
‘g’ 绿色 green
’ r ’ 红色 red
’ c ’ 青色 cyan
’ m ’ 品红 magenta
’ y ’ 黄色 yellow
’ k ’ 黑色 black
’ w ’ 白色 white

颜色参考地址:http://tools.jb51.net/color/jPicker

  • 2). alpha: 0-1,透明度
  • 3). linestyle:折线样式
字符 描述
‘-’ 实线
‘–’ 虚线
‘-.’ 点划线
‘:’ 虚线
  • 3). marker标记点:
标记符号 描述
‘.’ 点标记
‘o’ 圆圈标记
‘x’ 'X’标记
‘D’ 钻石标记
‘H’ 六角标记
‘s’ 正方形标记
‘+’ 加号标记
x= np.arange(0, 100,10)
y= x ** 2
"""linewidth 设置线条粗细
   label 设置线条标签
   color 设置线条颜色
   linestyle 设置线条形状
   marker 设置线条样点标记
"""
plt.plot(x, y, linewidth = '2', label = "test", color='b', linestyle='--', marker='H')
plt.legend(loc='upper left')

在这里插入图片描述

(2).线条样式缩写
# 颜色 标记 样式
plt.plot([1,2,3],[4,7,6],'r*-.')
plt.plot([2,4,5],[3,8,7],'m+--')

在这里插入图片描述

plt.rcParams['figure.figsize']=(8,4)
#不同种类不同颜色的线
#不同种类不同颜色的线并添加图例
x=np.linspace(0,10,100)

plt.plot(x,x+0, '-g', label='-g')    #实线  绿色

plt.plot(x,x+1, '--c', label='--c')   #虚线 浅蓝色

plt.plot(x,x+2, '-.k', label='-.k')   #点划线 黑色

plt.plot(x,x+3, '-r', label='-r')    #实线  红色

plt.plot(x,x+4, 'o', label='o')     #点   默认是蓝色

plt.plot(x,x+5, 'x', label='x')     #叉叉  默认是蓝色

plt.plot(x,x+6, 'dr', label='dr')    #砖石  红色

#添加图例右下角lower right  透明度  阴影  边框宽度
plt.legend(loc='lower right',framealpha=0.5,shadow=True, borderpad=0.5)

在这里插入图片描述

相关推荐

  1. 图片自适应各种设备尺寸

    2024-07-12 23:56:03       52 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-12 23:56:03       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-12 23:56:03       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-12 23:56:03       57 阅读
  4. Python语言-面向对象

    2024-07-12 23:56:03       68 阅读

热门阅读

  1. React@16.x(56)Redux@4.x(5)- 实现 createStore

    2024-07-12 23:56:03       21 阅读
  2. leetcode热题100.零钱兑换(动态规划)

    2024-07-12 23:56:03       18 阅读
  3. 跟我从零开始学STL(STL代码基础02)---vector容器

    2024-07-12 23:56:03       18 阅读
  4. 数据结构第18节 散列表 - 应用

    2024-07-12 23:56:03       21 阅读
  5. C# Modbus

    2024-07-12 23:56:03       21 阅读
  6. 安卓热门面试题一

    2024-07-12 23:56:03       19 阅读
  7. React组件间通信的几种方式

    2024-07-12 23:56:03       18 阅读
  8. TCP/IP模型和OSI模型的区别(面试题)

    2024-07-12 23:56:03       20 阅读
  9. opencv--把cv::Mat数据转为二进制数据的保存和读取

    2024-07-12 23:56:03       19 阅读