如何更改另一个变量的 pandas 图的线宽满足条件
How to change line width of a pandas plot if another variable satisfies a condtition
我想绘制一系列数据:
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)
绘制“0”和“1”系列,当 "ind0" 为 1 时,“0”的线宽增加,“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
我不熟悉 pandas DataFrame
s 如何在小规模上工作,但它们与 numpy ndarray
s 兼容就足够了。所以我假设你有后者,因为我的观点只是你应该根据变量 ind0
和 ind1
屏蔽你的值。我建议使用仅带有标记的 plt.plot
,(或者,等效地,plt.scatter
):
import numpy as np
import matplotlib.pyplot as 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]) # for plotting
trueinds0 = ind0.astype(bool) # for readibility
trueinds1 = ind1.astype(bool) # for readibility
lw_wide = 3 # larger linewidth
lw_narrow = 1 # smaller linewidth
hf,ax = plt.subplots()
# plot first column of s with indexing from 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 with indexing from 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 and two marker sizes:
size_wide = 50
size_narrow = 25
hf,ax = plt.subplots()
# create a single array specifying the marker sizes:
sizes = np.where(trueinds0,size_wide,size_narrow)
opts = {'c':'b','marker':'s','s':sizes,'edgecolors':'face'}
# plot first column of s with indexing from 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 with indexing from ind1
ax.scatter(srange,s[:,1],**opts)
由于其更简洁的形式,我建议使用后一种解决方案,scatter
。
的结果
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])
是:
我想绘制一系列数据:
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)
绘制“0”和“1”系列,当 "ind0" 为 1 时,“0”的线宽增加,“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
我不熟悉 pandas DataFrame
s 如何在小规模上工作,但它们与 numpy ndarray
s 兼容就足够了。所以我假设你有后者,因为我的观点只是你应该根据变量 ind0
和 ind1
屏蔽你的值。我建议使用仅带有标记的 plt.plot
,(或者,等效地,plt.scatter
):
import numpy as np
import matplotlib.pyplot as 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]) # for plotting
trueinds0 = ind0.astype(bool) # for readibility
trueinds1 = ind1.astype(bool) # for readibility
lw_wide = 3 # larger linewidth
lw_narrow = 1 # smaller linewidth
hf,ax = plt.subplots()
# plot first column of s with indexing from 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 with indexing from 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 and two marker sizes:
size_wide = 50
size_narrow = 25
hf,ax = plt.subplots()
# create a single array specifying the marker sizes:
sizes = np.where(trueinds0,size_wide,size_narrow)
opts = {'c':'b','marker':'s','s':sizes,'edgecolors':'face'}
# plot first column of s with indexing from 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 with indexing from ind1
ax.scatter(srange,s[:,1],**opts)
由于其更简洁的形式,我建议使用后一种解决方案,scatter
。
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])
是: