Matplotlib 图表的样式参数
1.
import numpy as np import pandas as pd import matplotlib.pyplot as plt % matplotlib inline # 导入相关模块 # linestyle参数 plt.plot([i**2 for i in range(100)], linestyle = '-.') #不用设置的时候默认为直线‘-’ # '-' solid line style # '--' dashed line style # '-.' dash-dot line style # ':' dotted line style
输出:
[<matplotlib.lines.Line2D at 0x1e1348c8550>]
2.
df = pd.DataFrame(np.random.randn(1000)) df.plot(kind = 'kde',linestyle = '--') #绘制密度图 df.hist() #绘制直方图
输出:
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x00000185B8B0ADA0>]], dtype=object)
3.
# marker参数 s = pd.Series(np.random.randn(100).cumsum()) s.plot(linestyle = '--',linewidth = 1, marker = 'x') #图中点的风格设置 默认为‘.’ linwidth为线宽 即线的粗细 # '.' 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
输出:
<matplotlib.axes._subplots.AxesSubplot at 0x185b8faed30>
5.
x = np.random.randn(1000) y = np.random.randn(1000) plt.scatter(x,y,marker = '.')
输出:
6.
# color参数 plt.hist(np.random.randn(100), color = 'g',alpha = 0.8) # alpha:0-1,透明度 # 常用颜色简写:red-r, green-g, black-k, blue-b, yellow-y df = pd.DataFrame(np.random.randn(1000, 4),columns=list('ABCD')) df = df.cumsum() df.plot(style = '--.',alpha = 0.8,colormap = 'GnBu') # colormap:颜色板,包括: # Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r, CMRmap, CMRmap_r, Dark2, Dark2_r, GnBu, GnBu_r, Greens, Greens_r, # Greys, Greys_r, OrRd, OrRd_r, Oranges, Oranges_r, PRGn, PRGn_r, Paired, Paired_r, Pastel1, Pastel1_r, Pastel2, Pastel2_r, PiYG, PiYG_r, # PuBu, PuBuGn, PuBuGn_r, PuBu_r, PuOr, PuOr_r, PuRd, PuRd_r, Purples, Purples_r, RdBu, RdBu_r, RdGy, RdGy_r, RdPu, RdPu_r, RdYlBu, RdYlBu_r, # RdYlGn, RdYlGn_r, Reds, Reds_r, Set1, Set1_r, Set2, Set2_r, Set3, Set3_r, Spectral, Spectral_r, Wistia, Wistia_r, YlGn, YlGnBu, YlGnBu_r, # YlGn_r, YlOrBr, YlOrBr_r, YlOrRd, YlOrRd_r, afmhot, afmhot_r, autumn, autumn_r, binary, binary_r, bone, bone_r, brg, brg_r, bwr, bwr_r, # cool, cool_r, coolwarm, coolwarm_r, copper, copper_r, cubehelix, cubehelix_r, flag, flag_r, gist_earth, gist_earth_r, gist_gray, gist_gray_r, # gist_heat, gist_heat_r, gist_ncar, gist_ncar_r, gist_rainbow, gist_rainbow_r, gist_stern, gist_stern_r, gist_yarg, gist_yarg_r, gnuplot, # gnuplot2, gnuplot2_r, gnuplot_r, gray, gray_r, hot, hot_r, hsv, hsv_r, inferno, inferno_r, jet, jet_r, magma, magma_r, nipy_spectral, # nipy_spectral_r, ocean, ocean_r, pink, pink_r, plasma, plasma_r, prism, prism_r, rainbow, rainbow_r, seismic, seismic_r, spectral, # spectral_r ,spring, spring_r, summer, summer_r, terrain, terrain_r, viridis, viridis_r, winter, winter_r # 其他参数见“颜色参数.docx”
输出:
<matplotlib.axes._subplots.AxesSubplot at 0x1e135dac4a8>
6.
# style参数,可以包含linestyle,marker,color ts = pd.Series(np.random.randn(1000).cumsum(), index=pd.date_range('1/1/2000', periods=1000)) ts.plot(style = '--g.',grid = True) # style → 风格字符串,这里包括了linestyle(-),marker(.),color(g) # plot()内也有grid参数
输出:
<matplotlib.axes._subplots.AxesSubplot at 0x1e135e04fd0>
7.
# 整体风格样式 import matplotlib.style as psl print(plt.style.available) # 查看样式列表 psl.use('ggplot') ts = pd.Series(np.random.randn(1000).cumsum(), index=pd.date_range('1/1/2000', periods=1000)) ts.plot(style = '--g.',grid = True,figsize=(10,6)) # 一旦选用样式后,所有图表都会有样式,重启后才能关掉
输出:
['seaborn-paper', 'seaborn-deep', 'fivethirtyeight', 'seaborn-pastel', 'dark_background',
'seaborn-bright', 'seaborn-ticks', 'seaborn-notebook', 'classic', 'seaborn-white',
'grayscale', 'seaborn-muted', 'seaborn-darkgrid', 'seaborn-dark', 'seaborn-talk',
'seaborn-colorblind', 'seaborn-dark-palette', 'ggplot', 'seaborn-whitegrid', 'seaborn-poster', 'bmh']
<matplotlib.axes._subplots.AxesSubplot at 0x1e134aef940>