Python绘图一条龙--大小/坐标/标签/多图布局/配色方案等


什么都不调,生成函数直接画的效果如下(颜色和配置会受默认参数影响),后面所有代码都直接往这段地下加就可以,需要一起运行(新手不要一行行跑)

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
def sinplot(flip=1):
    x = np.linspace(0, 14, 100)
    for i in range(1, 7):
        plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip)
sinplot()

1 画布设置

1.1 图片大小

将图片设置为宽 12 高 6 的格式:

sns.set(rc={"figure.figsize": (12, 6)}) # 宽 12, 高 6
sinplot()

1.2 背景格纹调整

格纹的颜色和粗细都可以调整,下图为黑色0.5磅,挺丑的,只是为了演示明显一点,一般0.1就可以

plt.grid(c='black',linewidth=0.5)

2 图片设置

2.1 背景边框设定

当用sns.set_style(“white”) 将背景设定成白色时,可能会需要去掉两侧框线,可使用sns.despine()去掉右上两条线。去框线前的四条线

sinplot()
sns.despine() # 默认去掉右边和上边框线
#sns.despine(left=True) # 指定隐去左边框线
#sns.despine(right=True) # 指定隐去右边框线

去掉右边和上边框线后,得到下图:

2.2 坐标轴与标签设置

  • 坐标轴
# 标注坐标轴名称,设定字体大小为14磅
plt.xlabel('I am the x-axis',fontsize = 14)
plt.ylabel('I am the y-axis',fontsize = 14)
  • 标签
    标签可以随心所欲,想在什么位置标注都可以,想让他叫什么也都可以,不过好像中文不会显示,只能用英文了。
# 对y标签指定位置命名名字
plt.yticks( [-6,-3,0,3,6],['LOWER','low', 'balance', 'high','HIGHER'])
plt.xticks([0,1,3,4,7,11,14])

2.3 线型设定(粗细、样式、maker形状等)

可以在原来设定的画图参数中,加入对曲线样式、粗细的设定

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
def sinplot(flip=1):
    x = np.linspace(0, 14, 100)
    i = 0; plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip,'.')
    i = i + 1; plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip,'*')
    i = i + 1; plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip,'.-')
    i = i + 1; plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip,'-')
    i = i + 1; plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip,'--')
    i = i + 1; plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip,'+')    
sinplot()

2.3 多图绘制

2.3.1 用 Matplotlib 绘图的多图设置

2.3.1.1 基于Figure
import matplotlib.pyplot as plt
import numpy as np
#通过对象绘图
fig=plt.figure()
ax1=fig.add_subplot(2,2,1)
ax2=fig.add_subplot(2,2,2)
ax3=fig.add_subplot(2,1,2)
x=np.linspace(-2*np.pi,2*np.pi)

#绘制第一幅图
Y1=np.sin(x)
ax1.plot(x,Y1,label=["Sin(X)"],color='r')
ax1.legend(loc="best",ncol=0)
ax1.grid(color='k')
#绘制第二幅图
Y2=np.cos(x)
ax2.plot(x,Y2,color='green',linewidth=5)
ax2.set_title("Cos(X)")
ax2.set_xlabel("X")
ax2.set_ylabel("Y")

#绘制第三幅图
Y3=np.tan(x)
ax3.plot(x,Y3,'ro--',linewidth=8)
ax3.set_xlabel('X')
ax3.set_ylabel('Y')
ax3.set_title("tan(X)")
ax3.set_xlim(-4*np.pi,4*np.pi)
#显示图像
plt.show()
2.3.1.2 用 subplot(m,n,k) 分区
import matplotlib.pyplot as plt
import numpy as np
#普通方式
X=np.linspace(-2*np.pi,2*np.pi)

#绘制第一幅图
plt.subplot(2,2,1)
Y1=np.sin(X)
plt.plot(X,Y1,label=["Sin(X)"],color='r')
plt.legend(loc="best",ncol=1)
plt.grid(color='k')

#绘制第二幅图
plt.subplot(2,2,2)
Y2=np.cos(X)
plt.plot(X,Y2,color='g',linewidth=2)
plt.title("Cos(X)")
plt.xlabel("X")
plt.ylabel("Y")
plt.grid(color='b')

#绘制第三幅图
plt.subplot(2,1,2)
Y3=np.tan(X)
plt.plot(X,Y3)
plt.xlabel("X")
plt.ylabel("Y")
plt.title("tan(X)")
plt.xlim(-4*np.pi,4*np.pi)
plt.grid(color='r')

#子图画完后一起show
plt.show()

2.3.2 用 Seaborn 绘图的多图设置

Seaborn 画的图确实比 Matplotlib 自己画的好看很多。

2.3.2.1 seaborn 指定坐标多图绘制

Seaborn 指定画图坐标ax绘制子图与plt直接绘图有一定差别,具体代码如下。

#fig, axs = plt.subplots(nrows=5)
fig,[ax1,ax2] = plt.subplots(2,1,figsize=(20,10))#图片布局行数&列数、副图坐标和整个图片大小设置
fig.subplots_adjust(hspace=0.4)# 图片间距调整

sns.lineplot(data=close, ax=ax1)
ax1.set_title('The trend of A share index and 4 important stock indexs')
sns.lineplot(data=df180, ax=ax2)
ax2.set_title(r'The trend of A share index and ShangZheng180 index')
2.3.2.2 seaborn subplot(k,m,n)多图绘制

都跟前面差不多,我忘了为啥最早自己画图的时候,用seaborn和subplot就画不出来。在这儿直接上代码做示范,需要的小伙伴自取。

from numpy import * # in order to invoke functions sin(x) and cos(x)
import matplotlib.pyplot as plt
import seaborn as sns
x = np.linspace(-2*np.pi,2*np.pi)
plt.subplot(2,1,1)
sns.lineplot(x,sin(x))
plt.subplot(2,1,2)
sns.lineplot(x,cosx)
plt.show()

3 颜色与风格

3.1 背景风格

  • seaborn 有五种主题风格:darkgrid,whitegrid,dark,white,ticks

    • whitegrid: 白底,黑色网格线
    • white:纯白底
    • darkgrid:灰底,白色网格线
    • dark:灰色纯色底
    • ticks:感觉跟white一样啊,没看出来区别…
  • 具体代码:

    style = ‘whitegrid’ / ‘white’ / ’ darkgrid’ / ‘dark’ / ‘ticks’
    sns.set_style(style)

    (1) whitegrid风格:

style = 'whitegrid' #可选范围:white,darkgrid,dark,ticks
sns.set_style(style)
sinplot()
(2) white 风格
style = 'white' #可选范围:white,darkgrid,dark,ticks
sns.set_style(style)
sinplot()
其他 styles 自己试一下就知道啦。

3.2 颜色设定

3.2.1 基于调色板的颜色设定

  • color_palette:

    • matplotlib 默认色板:sns.palplot(sns.color_palette(“cubehelix”, 8))
    • 浅到深 渐变蓝:sns.palplot(sns.color_palette(“Blues”))
    • 深到浅_r 渐变绿 :sns.palplot(sns.color_palette(“BuGn_r”))
    • 暗处理_d 渐变绿:sns.palplot(sns.color_palette(“GnBu_d”))
    • 黄绿配色 离散色板:sns.palplot(sns.color_palette(“BrBG”, 7))
    • 蓝红配色 离散色板:sns.palplot(sns.color_palette(“RdBu_r”, 7))
  • 查看色版:sns.palplot(color_palette)
    在这里插入图片描述

  • 色板的应用可以使用 sis 模块中 set_palette 函数来设定模版,该函数参数与 color_palette 的参数相同

    • 示例:浅到深 渐变蓝
sns.set_palette("Blues")
sinplot()

在这里插入图片描述

3.2.2 基于 RGB 的颜色设定

RGB通过对红(Red)、绿(Green)、蓝(Blue)三个颜色不同的配比来得到各式各样的颜色。一个颜色用六位字母/数字表示,前两位表示红色的多少,中间两位表示绿色的多少,最后两位表示蓝色的多少(#000000表示三个颜色通道都没有,所以是白色)。在 RGB 中,用 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F 来表示大小,0最小F最大。

单色不同明暗:
#FF0000, #AA0000, #700000, #550000,
#00FF00, #00CC00, #009900, #002200,
#0000FF, #0000E7, #00007E, #000077,
混色:
#AA00AA , #00AAAA, #FAAA00
贴几个我常用的颜色:
DarkGoldenrod2 #FFB90F
MediumSeaGreen #3CB371
Steelblue #4682B4,
DeepPink #FF1493
Firebrick #B22222

RGB 颜色可以搭配出很多,具体可以从色表里查询,举个例子:【色表查询】https://www.sojson.com/rgb.html

3.2.3 配色方案

3.2.3.1 三色搭配(1)

在这里插入图片描述

绿
77,133,189247,144,6189,169,90
#4D85BD#F7903D#59A95A
3.2.3.2 三色搭配 (2)

在这里插入图片描述

深蓝灰蓝
201,32,3956,89,137127,165,183
#D22027#385989#7FA5B7
3.2.3.3 四色搭配:

在这里插入图片描述

深蓝灰绿浅黄
37,73,11570,190,6045,80,115255,215,110
#254973#46BEA0#F55073#FFD76E
3.2.3.4 五色搭配:降低饱和度,不然会显脏

在这里插入图片描述

深蓝姜黄橘黄蓝绿深灰
1,86,153250,192,15243,118,7495,198,20179,89,100
#015699#FAC00F#F3764A#5FC6C9#4F5964
  • 10
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值