Matplotlib&Pyecharts样式的简单设置

一、Matplotlib样式设置

1.1.设置图片

1
plt.figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True, FigureClass=<class 'matplotlib.figure.Figure'>, clear=False, **kwargs)
1
2
3
4
5
6
7
import matplotlib.pyplot as plt 

plt.figure(figsize=(3,3),dpi=100,facecolor='red',edgecolor='green',linewidth=5)

plt.plot([1,2,3,4,5])

plt.show()

1.2.设置标题

1
plt.title(label, fontdict=None, loc=None, pad=None, \*, y=None, \*\*kwargs)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import matplotlib.pyplot as plt  

x = [-5,-4,-3,-2,-1,0,1,2, 3, 4, 5]
y = []

for i in range(len(x)):
y.append(max(0,x[i]))

plt.plot(x, y)

plt.title(label="ReLU function graph",
fontsize=20,
color="green",
pad='20.0',
loc="left")
plt.show()

1.3.设置多图总标题

1
plt.suptitle(t, **kwargs)
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
import numpy as np
import matplotlib.pyplot as plt

m1=1
c1=0

m2 = 2
c2 = 2

m3 = 2
c3 = 1

m4 = 1
c4 = 2

x=np.linspace(0,3,100)
y1 = m1*x + c1
y2 = m2*x + c2
y3 = m3*x + c3
y4 = m4*x + c4

fig, ax = plt.subplots(2, 2,figsize=(10, 8))

ax[0, 0].plot(x, y1)
ax[0, 1].plot(x, y2)
ax[1, 0].plot(x, y3)
ax[1, 1].plot(x,y4)

ax[0, 0].set_title("Line-1")
ax[0, 1].set_title("Line-2")
ax[1, 0].set_title("Line-3")
ax[1, 1].set_title("Line-4")

plt.suptitle('Various Straight Lines',fontsize=20, ha = 'left',color='blue')

plt.show()

1.4.设置图例

1
plt.legend(*args, **kwargs)
1
2
3
4
5
6
7
8
9
10
11
12
import numpy as np 
import matplotlib.pyplot as plt

y1 = [2, 3, 4.5]
y2 = [1, 1.5, 5]

plt.plot(y1)
plt.plot(y2)

plt.legend(["Line1", "Line2"], loc ="upper center",ncol = 2,fontsize=13)

plt.show()

1.5.设置轴样式

xlabel:设置X轴的标签。

xlim:设置当前轴的X范围。

xticks:设置X轴的当前刻度位置和标签。

ylabel:设置Y轴的标签。

ylim:设置当前轴的Y范围。

yticks:设置Y轴的当前刻度位置和标签。

1
plt.axis(*args, emit=True, **kwargs)
1
2
3
4
5
6
7
8
9
10
import matplotlib.pyplot as plt 

x =[1, 2, 3, 4, 5]
y =[2, 4, 6, 8, 10]

plt.plot(x, y)

plt.axis([0, 10, 1, 15])

plt.show()

1.5.1.显示次要刻度

1
plt.minorticks_on()

1.5.2.不显示次要刻度

1
plt.minorticks_off()
1
2
3
4
5
6
7
8
9
10
11
import matplotlib.pyplot as plt 
import numpy as np

x = np.arange(0.0, 2, 0.01)
y1 = np.sin(2 * np.pi * x)
y2 = 1.2 * np.sin(4 * np.pi * x)

plt.fill_between(x, y1, y2, color ="green", alpha = 0.6)

plt.minorticks_on()
plt.show()

1.6.设置网格线

1
plt.grid(b=None, which='major', axis='both', **kwargs)
1
2
3
4
5
6
import matplotlib.pyplot as plt 

plt.plot([1,2,3,4,5])
plt.grid(axis='x',ls='--',color='r')

plt.show()

1.7.设置标注线、面、注释

1
plt.axhline(y=0, xmin=0, xmax=1, **kwargs)
1
plt.axvline(x=0, ymin=0, ymax=1, **kwargs)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import numpy as np 
import matplotlib.pyplot as plt

t = np.linspace(-10, 10, 100)
sig = 1 / t

plt.axhline(y = 0, color ="green", linestyle ="--")
plt.axhline(y = 0.5, color ="green", linestyle =":")
plt.axhline(y = 1.0, color ="green", linestyle ="--")
plt.axvline(color ="black")

plt.plot(t, sig, label = r"$\sigma(t) = \frac{1}{x}$")

plt.legend()

plt.show()
1
plt.axhspan(xmin, xmax, ymin=0, ymax=1, **kwargs)
1
plt.axvspan(ymin, ymax, xmin=0, xmax=1, **kwargs)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import numpy as np 
import matplotlib.pyplot as plt

t = np.linspace(-10, 10, 100)
sig = 1 / t

plt.axhspan(0, 1, facecolor ="green", alpha = 0.2)
plt.axvspan(0, 1, facecolor ="red", alpha = 0.2)

plt.plot(t, sig, label = r"$\sigma(t) = \frac{1}{x}$")

plt.legend()

plt.show()

1
plt.annotate(text, xy, *args, **kwargs)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import matplotlib.pyplot as plt 
import numpy as np


t = np.arange(0.0, 5.0, 0.001)
s = np.cos(3 * np.pi * t)
plt.plot(t, s, lw = 2)

plt.annotate('Local Max', xy =(3.3, 1),
xytext =(2.9, 1.5),
arrowprops = dict(facecolor ='green'))

plt.ylim(-2, 2)
plt.show()

1.8.设置文字

1
plt.text(x, y, s, fontdict=None, **kwargs)
1
2
3
4
5
6
7
8
9
10
11
import matplotlib.pyplot as plt 

x =[1, 2, 3, 4, 5]
y =[2, 4, 6, 8, 10]


plt.bar(x, y)

plt.text(2, 4.5, "4",fontsize=15,color='r')

plt.show()

1.9.设置箭头

1
plt.arrow(x, y, dx, dy, **kwargs)
1
2
3
4
5
6
7
8
9
10
11
import matplotlib.pyplot as plt 

x =[1, 2, 3, 4, 5]
y =[2, 4, 6, 8, 10]


plt.bar(x, y)

plt.arrow(1.7, 5, 0.3,-1, width = 0.05,color='r')

plt.show()

1.10.更多


二、Pyecharts样式设置

pyecharts使用配置项设置样式,分为全局配置项和系列配置项,函数分别为set_global_opts()set_series_opts()

2.1.初始化设置

设置画布的宽度高度、id、渲染方式(canvas、svg)、网页标题、图表主题配色图表背景颜色、画图动画初始化配置

1
InitOpts()
1
2
3
4
5
6
7
8
9
10
11
12
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
from pyecharts.globals import ThemeType


c = (
Bar(init_opts=opts.InitOpts(width='500px',height='300px',theme=ThemeType.WALDEN,bg_color='#FFEEDD'))
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
)
c.render_notebook()

2.2.设置标题

设置主标题(可换行)、标题跳转链接、跳转方式(’self’, ‘blank’)、副标题(可换行)、副标题跳转链接、副标题跳转方式、标题位置(上下左右)、标题内边距、主副标题之间的间距、主标题字体样式配置项、副标题字体样式配置项

1
TitleOpts()
1
2
3
4
5
6
7
8
9
10
11
12
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker


c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.set_global_opts(title_opts=opts.TitleOpts(title='柱状图',subtitle='这是一个柱状图的例子',pos_left='right',pos_top='20%'))
)
c.render_notebook()

2.3.设置图例

设置图例的类型(’plain’平铺、’scroll’滚动)、图例选择的模式、是否显示图例组件图例位置(上下左右)图例列表的布局朝向(水平、垂直)、图例标记和文本的对齐、图例内边距、图例每项之间的间隔图例标记的图形大小(宽度、高度)、图例关闭时的颜色、图例组件字体样式、图例项的 icon(’circle’, ‘rect’, ‘roundRect’, ‘triangle’, ‘diamond’, ‘pin’, ‘arrow’, ‘none’,或其他任意图片)

1
LegendOpts()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker


c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_global_opts(legend_opts=opts.LegendOpts(
orient='vertical',pos_left='right',item_gap=20,item_width=30,item_height=20,legend_icon='pin'))
)
c.render_notebook()

2.4.设置轴样式

设置坐标轴类型(’value’、’category’、’time’、’log’)坐标轴名称是否显示轴是否是脱离 0 值比例是否反向坐标轴、坐标轴名称显示位置、坐标轴名称与轴线之间的距离、坐标轴名字旋转、强制设置坐标轴分割间隔、轴所在的 grid 的索引、X轴的位置、Y轴相对于默认位置的偏移、坐标轴的分割段数、坐标轴两边留白策略坐标轴刻度最小值、坐标轴刻度最大值、坐标轴最小间隔、坐标轴最大间隔、坐标轴刻度线配置项(AxisLineOpts)、坐标轴刻度配置项(AxisTickOpts)、坐标轴标签配置项(LabelOpts)、坐标轴指示器配置项(AxisPointerOpts)、坐标轴名称的文字样式、分割区域配置项(SplitAreaOpts)、分割线配置项(SplitLineOpts)、坐标轴次刻度线相关设置(MinorTickOpts)、坐标轴在 grid 区域中的次分隔线(MinorSplitLineOpts)

1
AxisOpts()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import random
from pyecharts import options as opts
from pyecharts.charts import Scatter
from pyecharts.faker import Faker

datax=[]
datay1=[]
datay2=[]
for i in range(20):
datax.append(random.uniform(-1,1))
datay1.append(random.uniform(1,2))
datay2.append(random.uniform(1,2))

c = (
Scatter()
.add_xaxis(datax)
.add_yaxis("数据1", datay1)
.add_yaxis("数据2", datay2)
.set_global_opts(xaxis_opts=opts.AxisOpts(type_='value',name='数据值x',position='top',split_number=10,max_=1.5,min_=-1.5),
yaxis_opts=opts.AxisOpts(name='数据值y',is_scale=True,is_inverse=True,boundary_gap=['20%','20%'],min_interval=0.5)
)
.set_series_opts(label_opts = opts.LabelOpts(is_show=False))
)
c.render_notebook()

2.5.坐标轴刻度线配置项(AxisLineOpts)

设置是否显示轴线、X 轴或者 Y 轴的轴线是否在另一个轴的 0 刻度上、轴线两边的箭头坐标轴线风格配置项(LineStyleOpts)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import random
from pyecharts import options as opts
from pyecharts.charts import Scatter

datax=[]
datay=[]
for i in range(20):
datax.append(random.uniform(0,1))
datay.append(random.uniform(0,1))

c = (
Scatter()
.add_xaxis(datax)
.add_yaxis("数据", datay)
.set_global_opts(xaxis_opts=opts.AxisOpts(type_='value',
axisline_opts=opts.AxisLineOpts(symbol='arrow')))
.set_series_opts(label_opts = opts.LabelOpts(is_show=False))
)
c.render_notebook()

2.6.坐标轴刻度配置项(AxisTickOpts)

设置是否显示坐标轴刻度坐标轴刻度是否朝内坐标轴刻度的长度,坐标轴线风格配置项(LineStyleOpts)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import random
from pyecharts import options as opts
from pyecharts.charts import Scatter

datax=[]
datay=[]
for i in range(20):
datax.append(random.uniform(0,1))
datay.append(random.uniform(0,1))

c = (
Scatter()
.add_xaxis(datax)
.add_yaxis("数据", datay)
.set_global_opts(xaxis_opts=opts.AxisOpts(type_='value',
axistick_opts=opts.AxisTickOpts(is_inside=True,length=10)))
.set_series_opts(label_opts = opts.LabelOpts(is_show=False))
)
c.render_notebook()

2.7.分割区域配置项(SplitAreaOpts)

设置图形透明度填充的颜色

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import random
from pyecharts import options as opts
from pyecharts.charts import Scatter

datax=[]
datay=[]
for i in range(20):
datax.append(random.uniform(0,1))
datay.append(random.uniform(0,1))

c = (
Scatter()
.add_xaxis(datax)
.add_yaxis("数据", datay)
.set_global_opts(xaxis_opts=opts.AxisOpts(type_='value',
splitarea_opts=opts.SplitAreaOpts(
areastyle_opts=opts.AreaStyleOpts(opacity=0.5,
color='#FFEEDD'))))
.set_series_opts(label_opts = opts.LabelOpts(is_show=False))
)
c.render_notebook()

2.8.分割线配置项(SplitLineOpts)

设置背景网格线,是否显示,线型样式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import random
from pyecharts import options as opts
from pyecharts.charts import Scatter

datax=[]
datay=[]
for i in range(20):
datax.append(random.uniform(0,1))
datay.append(random.uniform(0,1))

c = (
Scatter()
.add_xaxis(datax)
.add_yaxis("数据", datay)
.set_global_opts(xaxis_opts=opts.AxisOpts(type_='value',splitline_opts=opts.SplitLineOpts(is_show=True)),
yaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True)))
.set_series_opts(label_opts = opts.LabelOpts(is_show=False))
)
c.render_notebook()

2.9.坐标轴次刻度线相关设置(MinorTickOpts)

设置次刻度线是否显示次刻度线分割数次刻度线的长度,次刻度线的样式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import random
from pyecharts import options as opts
from pyecharts.charts import Scatter

datax=[]
datay=[]
for i in range(20):
datax.append(random.uniform(0,1))
datay.append(random.uniform(0,1))

c = (
Scatter()
.add_xaxis(datax)
.add_yaxis("数据", datay)
.set_global_opts(xaxis_opts=opts.AxisOpts(type_='value',minor_tick_opts=opts.MinorTickOpts(is_show=True,split_number=10,length=4)))
.set_series_opts(label_opts = opts.LabelOpts(is_show=False))
)
c.render_notebook()

2.10.设置视觉映射(颜色对应的数值)

是否显示视觉映射配置,映射过渡类型(”color”, “size”),指定 visualMapPiecewise 组件的最小值、最大值,visualMap 组件过渡颜色,visualMap 组件过渡 symbol 大小,visualMap 图元以及其附属物(如文字标签)的透明度,如何放置 visualMap 组件(’horizontal’,’vertical’),visualMap 组件离容器的距离(上下左右),指定取哪个系列的数据……

1
VisualMapOpts(视觉映射配置项)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import random
from pyecharts import options as opts
from pyecharts.charts import Scatter

datax=[]
datay1=[]
datay2=[]
for i in range(10):
datax.append(random.uniform(0,1))
datay1.append(random.uniform(0,1))
datay2.append(random.uniform(0,1))

c = (
Scatter()
.add_xaxis(datax)
.add_yaxis("数据1", datay1)
.add_yaxis("数据2", datay2)
.set_global_opts(xaxis_opts=opts.AxisOpts(type_='value'), visualmap_opts=opts.VisualMapOpts(series_index=1,max_=1,orient='horizontal',item_width=20,item_height=200))
)
c.render_notebook()

2.11.设置缩放

设置是否显示缩放,缩放类型(”slider”, “inside”),布局方式(’horizontal’, ‘vertical’),位置(上下左右)……

1
DataZoomOpts(区域缩放配置项)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import random
from pyecharts import options as opts
from pyecharts.charts import Scatter

datax=[]
datay1=[]

for i in range(10):
datax.append(random.uniform(0,1))
datay.append(random.uniform(0,1))

c = (
Scatter()
.add_xaxis(datax)
.add_yaxis("数据1", datay)
.set_global_opts(xaxis_opts=opts.AxisOpts(type_='value'),
datazoom_opts=[opts.DataZoomOpts(orient="vertical"), opts.DataZoomOpts(type_="inside")])
)
c.render_notebook()

2.12.设置标注点、线、面

1
MarkPointOpts:标记点配置项
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker

x, y = Faker.choose(), Faker.values()
c = (
Bar()
.add_xaxis(x)
.add_yaxis(
"商家A",
y,
markpoint_opts=opts.MarkPointOpts(
data=[opts.MarkPointItem(name="自定义标记点", coord=[x[2], y[2]+10], value=y[2],symbol='circle')]
),
)
.add_yaxis("商家B", Faker.values())
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
)
c.render_notebook()
1
MarkLineOpts:标记线配置项
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker

c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_series_opts(
label_opts=opts.LabelOpts(is_show=False),
markline_opts=opts.MarkLineOpts(
data=[opts.MarkLineItem(y=50, name="yAxis=50")]
),
)
)
c.render_notebook()
1
MarkAreaOpts: 标记区域配置项
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker

c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_series_opts(
label_opts=opts.LabelOpts(is_show=False),
markarea_opts=opts.MarkAreaOpts(
data=[opts.MarkAreaItem(y=[50,90], name="yAxis=50-90")]
),
)
)
c.render_notebook()

2.13.更多

2.13.1.设置工具箱

1
ToolboxOpts:工具箱配置项

2.13.2.设置过场动画

1
AnimationOpts:Echarts 画图动画配置项

2.13.3.设置提示框

1
TooltipOpts:提示框配置项

2.13.4.贴图

1
GraphicGroup:原生图形元素组件

三、尾巴