什么都不调,生成函数直接画的效果如下(颜色和配置会受默认参数影响),后面所有代码都直接往这段地下加就可以,需要一起运行(新手不要一行行跑)
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()
![](https://img-blog.csdnimg.cn/2020101821411667.png?watmark/2/text/aHR0cDovL2cuY3Nkbi5uZXQvV5bm1hbjIzMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
1 画布设置
1.1 图片大小
将图片设置为宽 12 高 6 的格式:
sns.set(rc={"figure.figsize": (12, 6)}) # 宽 12, 高 6
sinplot()
![](https://img-blog.csdnimg.cn/20201018214533933.png?watmark/2/text/aHR0cDovL2cuY3Nkbi5uZXQvV5bm1hbjIzMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
1.2 背景格纹调整
格纹的颜色和粗细都可以调整,下图为黑色0.5磅,挺丑的,只是为了演示明显一点,一般0.1就可以
plt.grid(c='black',linewidth=0.5)
![](https://img-blog.csdnimg.cn/20201019152904313.png?watmark/2/text/aHR0cDovL2cuY3Nkbi5uZXQvV5bm1hbjIzMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
2 图片设置
2.1 背景边框设定
当用sns.set_style(“white”) 将背景设定成白色时,可能会需要去掉两侧框线,可使用sns.despine()去掉右上两条线。去框线前的四条线
![](https://img-blog.csdnimg.cn/20201018220951920.png?watmark/2/text/aHR0cDovL2cuY3Nkbi5uZXQvV5bm1hbjIzMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
sinplot()
sns.despine() # 默认去掉右边和上边框线
#sns.despine(left=True) # 指定隐去左边框线
#sns.despine(right=True) # 指定隐去右边框线
去掉右边和上边框线后,得到下图:
![](https://img-blog.csdnimg.cn/20201018221118429.png?watmark/2/text/aHR0cDovL2cuY3Nkbi5uZXQvV5bm1hbjIzMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
2.2 坐标轴与标签设置
- 坐标轴
# 标注坐标轴名称,设定字体大小为14磅
plt.xlabel('I am the x-axis',fontsize = 14)
plt.ylabel('I am the y-axis',fontsize = 14)
![](https://img-blog.csdnimg.cn/20201019151920726.png?watmark/2/text/aHR0cDovL2cuY3Nkbi5uZXQvV5bm1hbjIzMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
- 标签
标签可以随心所欲,想在什么位置标注都可以,想让他叫什么也都可以,不过好像中文不会显示,只能用英文了。
# 对y标签指定位置命名名字
plt.yticks( [-6,-3,0,3,6],['LOWER','low', 'balance', 'high','HIGHER'])
plt.xticks([0,1,3,4,7,11,14])
![](https://img-blog.csdnimg.cn/20201019152406428.png?watmark/2/text/aHR0cDovL2cuY3Nkbi5uZXQvV5bm1hbjIzMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
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()
![](https://img-blog.csdnimg.cn/20201102103918386.png?watmark/2/text/aHR0cDovL2cuY3Nkbi5uZXQvV5bm1hbjIzMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
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()
![](https://img-blog.csdnimg.cn/20210322141941848.png?watmark/2/text/aHR0cDovL2cuY3Nkbi5uZXQvV5bm1hbjIzMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
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()
![](https://img-blog.csdnimg.cn/2021032214231727.png?watmark/2/text/aHR0cDovL2cuY3Nkbi5uZXQvV5bm1hbjIzMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
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')
![](https://img-blog.csdnimg.cn/2021032214150594.png?watmark/2/text/aHR0cDovL2cuY3Nkbi5uZXQvV5bm1hbjIzMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
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()
![](https://img-blog.csdnimg.cn/0f391fad180f431ea14f27fe41c4685f.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YyX5Yaw5rSL55qE5Za1,size_10,color_FFFFFF,t_70,g_se,x_16)
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()
![](https://img-blog.csdnimg.cn/20201018220707929.png?watmark/2/text/aHR0cDovL2cuY3Nkbi5uZXQvV5bm1hbjIzMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
style = 'white' #可选范围:white,darkgrid,dark,ticks
sns.set_style(style)
sinplot()
![](https://img-blog.csdnimg.cn/20201018220601837.png?watmark/2/text/aHR0cDovL2cuY3Nkbi5uZXQvV5bm1hbjIzMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
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,189 | 247,144,61 | 89,169,90 |
#4D85BD | #F7903D | #59A95A |
3.2.3.2 三色搭配 (2)
红 | 深蓝 | 灰蓝 |
---|---|---|
201,32,39 | 56,89,137 | 127,165,183 |
#D22027 | #385989 | #7FA5B7 |
3.2.3.3 四色搭配:
深蓝 | 灰绿 | 粉 | 浅黄 |
---|---|---|---|
37,73,115 | 70,190,60 | 45,80,115 | 255,215,110 |
#254973 | #46BEA0 | #F55073 | #FFD76E |
3.2.3.4 五色搭配:降低饱和度,不然会显脏
深蓝 | 姜黄 | 橘黄 | 蓝绿 | 深灰 |
---|---|---|---|---|
1,86,153 | 250,192,15 | 243,118,74 | 95,198,201 | 79,89,100 |
#015699 | #FAC00F | #F3764A | #5FC6C9 | #4F5964 |