python - How to change line width of a pandas plot if another variable satisfies a condtition -
i want plot series of data:
s = pd.dataframe(np.random.randn(5,2)) ind = pd.dataframe({'ind0':np.random.random_integers(0,1, 5), \ 'ind1':np.random.random_integers(0,1, 5)}) data = pd.concat([s,ind], axis=1)
where "0" , "1" series plot , line width increases "0" when "ind0" 1, , likewise "1".
0 1 ind0 ind1 0 2.029756 -1.211402 1 0 1 0.428830 0.508613 1 0 2 1.964346 1.032110 0 1 3 1.424997 -0.363719 1 0 4 -0.581283 0.774375 1 0
i'm not familiar how pandas dataframe
s work on small scale, it's enough compatible numpy ndarray
s. i'll assume have latter, point should mask values based on variables ind0
, ind1
. suggest using plt.plot
markers, (or, equivalently, plt.scatter
):
import numpy np import matplotlib.pyplot plt n = 10 s = np.random.randn(n,2) ind0 = np.random.random_integers(0,1, n) ind1 = np.random.random_integers(0,1, n) srange = np.arange(s.shape[0]) # plotting trueinds0 = ind0.astype(bool) # readibility trueinds1 = ind1.astype(bool) # readibility lw_wide = 3 # larger linewidth lw_narrow = 1 # smaller linewidth hf,ax = plt.subplots() # plot first column of s indexing ind0 ax.plot(srange[trueinds0],s[:,0][trueinds0],'bs',markeredgecolor='blue',markeredgewidth=lw_wide) ax.plot(srange[np.logical_not(trueinds0)],s[:,0][np.logical_not(trueinds0)],'bs',markeredgecolor='blue',markeredgewidth=lw_narrow) # plot second column of s indexing ind1 ax.plot(srange[trueinds1],s[:,1][trueinds1],'ro',markeredgecolor='red',markeredgewidth=lw_wide) ax.plot(srange[np.logical_not(trueinds1)],s[:,1][np.logical_not(trueinds1)],'ro',markeredgecolor='red',markeredgewidth=lw_narrow) ####### # using scatter , 2 marker sizes: size_wide = 50 size_narrow = 25 hf,ax = plt.subplots() # create single array specifying marker sizes: sizes = np.where(trueinds0,size_wide,size_narrow) opts = {'c':'b','marker':'s','s':sizes,'edgecolors':'face'} # plot first column of s indexing ind0 ax.scatter(srange,s[:,0],**opts) sizes = np.where(trueinds1,size_wide,size_narrow) opts = {'c':'r','marker':'o','s':sizes,'edgecolors':'face'} # plot second column of s indexing ind1 ax.scatter(srange,s[:,1],**opts)
due more concise form, suggest using latter solution, scatter
. result of with
ind0 = np.array([1, 0, 1, 1, 1, 0, 1, 1, 1, 0]) ind1 = np.array([0, 0, 0, 0, 0, 1, 0, 1, 0, 1])
is:
Comments
Post a Comment