带有字符串 X 和 Y 坐标的散点图
Scatter plot with string X and Y coordinates
我确实看到了 this,但我想知道是否有没有 pandas 的方法。
这是我试过的。
X_AXIS
和Y_AXIS
分别是xTickMarks
和yTickMarks
中包含的字符串。
X_AXIS
和 Y_AXIS
中的每个值都有对应的 Z_AXIS
值..散点的大小使用列表 FREQ
表示。
xTickMarks = ["0B", "1B", "2B", "1B3S", "2B2S"]
yTickMarks = ["-1S","0S", "1S", "2S", "3S", "4S"]
matplotlib.rc('font', serif='Helvetica Neue')
matplotlib.rc('text', usetex='false')
matplotlib.rcParams.update({'font.size': 10})
fig = plt.figure(figsize=(11.69,4.88)) # for landscape
axes1 = fig.add_subplot(111)
'''
Tuple of big,small cores with a tuple of power.
let x be big core
let y be small core
let z be Power
'''
plt.grid(True,linestyle='-',color='0.75')
x = X_AXIS
y = Y_AXIS
z = Z_AXIS
s = [int(FREQ[i])/1000000.0 for i in range(len(FREQ))]
plt.scatter(x,y,s=s,c=z, marker = 'o', cmap = cm.jet )
cb = plt.colorbar()
cb.set_label('Frequency', fontsize=20)
xtickNames = axes1.set_xticklabels(xTickMarks)
ytickNames = axes1.set_yticklabels(yTickMarks)
axes1.set_ylabel('Small cores')
axes1.set_xlabel('Big cores')
axes1.legend(prop={'size':5}, ncol=4)
axes1.xaxis.grid(True)
figsize=(11.69,8.27) # for landscape
fig.savefig(savepath + 'state-transition.png', bbox_inches='tight', dpi=300, pad_inches=0.1)
plt.clf()
当我运行这个时,plt.scatter
需要一个浮点值而不是一个字符串。
我想不用pandas
。
X_AXIS、Y_AXIS 和 Z_AXIS 的示例值:
X_AXIS = ['1B', '2B', '2B', '2B']
Y_AXIS = ['0S', '0S', '2S', '2S']
Z_AXIS = [1.5637257394958113, 1.5399805470086181, 1.4030363999998978, 1.4198133749999822]
散点图需要数字。您可以将字符串值映射到数字,然后设置刻度以匹配映射。
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.cm as cm
X_AXIS = ['1B', '2B', '2B', '1B3S']
Y_AXIS = ['0S', '0S', '2S', '2S']
Z_AXIS = [1.5637257394958113, 1.5399805470086181, 1.4030363999998978, 1.4198133749999822]
FREQ = [5000000.] * 4
xTickMarks = ["0B", "1B", "2B", "1B3S", "2B2S"]
yTickMarks = ["-1S","0S", "1S", "2S", "3S", "4S"]
matplotlib.rc('font', serif='Helvetica Neue')
matplotlib.rc('text', usetex='false')
matplotlib.rcParams.update({'font.size': 10})
fig = plt.figure(figsize=(11.69,4.88)) # for landscape
axes1 = fig.add_subplot(111)
'''
Tuple of big,small cores with a tuple of power.
let x be big core
let y be small core
let z be Power
'''
plt.grid(True,linestyle='-',color='0.75')
x = [xTickMarks.index(i) for i in X_AXIS]
y = [yTickMarks.index(i) for i in Y_AXIS]
z = Z_AXIS
s = [int(FREQ[i])/1000000.0 for i in range(len(FREQ))]
plt.scatter(x,y,s=s,c=z, marker = 'o', cmap = cm.jet )
cb = plt.colorbar()
cb.set_label('Frequency', fontsize=20)
axes1.set_xlim((0, len(xTickMarks)-1))
axes1.set_ylim((0, len(yTickMarks)-1))
axes1.set_xticks(xrange(len(xTickMarks)))
axes1.set_yticks(xrange(len(yTickMarks)))
axes1.set_xticklabels(xTickMarks)
axes1.set_yticklabels(yTickMarks)
axes1.set_ylabel('Small cores')
axes1.set_xlabel('Big cores')
axes1.legend(prop={'size':5}, ncol=4)
axes1.xaxis.grid(True)
figsize=(11.69,8.27) # for landscape
#fig.savefig('state-transition.png', bbox_inches='tight', dpi=300, pad_inches=0.1)
plt.show()
与您的代码相比,我在这里更改的是 x 和 y 的值:
x = [xTickMarks.index(i) for i in X_AXIS]
y = [yTickMarks.index(i) for i in Y_AXIS]
和你坐标轴的刻度:
axes1.set_xlim((0, len(xTickMarks)-1))
axes1.set_ylim((0, len(yTickMarks)-1))
axes1.set_xticks(xrange(len(xTickMarks)))
axes1.set_yticks(xrange(len(yTickMarks)))
axes1.set_xticklabels(xTickMarks)
axes1.set_yticklabels(yTickMarks)
希望这对您有所帮助。
我确实看到了 this,但我想知道是否有没有 pandas 的方法。
这是我试过的。
X_AXIS
和Y_AXIS
分别是xTickMarks
和yTickMarks
中包含的字符串。
X_AXIS
和 Y_AXIS
中的每个值都有对应的 Z_AXIS
值..散点的大小使用列表 FREQ
表示。
xTickMarks = ["0B", "1B", "2B", "1B3S", "2B2S"]
yTickMarks = ["-1S","0S", "1S", "2S", "3S", "4S"]
matplotlib.rc('font', serif='Helvetica Neue')
matplotlib.rc('text', usetex='false')
matplotlib.rcParams.update({'font.size': 10})
fig = plt.figure(figsize=(11.69,4.88)) # for landscape
axes1 = fig.add_subplot(111)
'''
Tuple of big,small cores with a tuple of power.
let x be big core
let y be small core
let z be Power
'''
plt.grid(True,linestyle='-',color='0.75')
x = X_AXIS
y = Y_AXIS
z = Z_AXIS
s = [int(FREQ[i])/1000000.0 for i in range(len(FREQ))]
plt.scatter(x,y,s=s,c=z, marker = 'o', cmap = cm.jet )
cb = plt.colorbar()
cb.set_label('Frequency', fontsize=20)
xtickNames = axes1.set_xticklabels(xTickMarks)
ytickNames = axes1.set_yticklabels(yTickMarks)
axes1.set_ylabel('Small cores')
axes1.set_xlabel('Big cores')
axes1.legend(prop={'size':5}, ncol=4)
axes1.xaxis.grid(True)
figsize=(11.69,8.27) # for landscape
fig.savefig(savepath + 'state-transition.png', bbox_inches='tight', dpi=300, pad_inches=0.1)
plt.clf()
当我运行这个时,plt.scatter
需要一个浮点值而不是一个字符串。
我想不用pandas
。
X_AXIS、Y_AXIS 和 Z_AXIS 的示例值:
X_AXIS = ['1B', '2B', '2B', '2B']
Y_AXIS = ['0S', '0S', '2S', '2S']
Z_AXIS = [1.5637257394958113, 1.5399805470086181, 1.4030363999998978, 1.4198133749999822]
散点图需要数字。您可以将字符串值映射到数字,然后设置刻度以匹配映射。
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.cm as cm
X_AXIS = ['1B', '2B', '2B', '1B3S']
Y_AXIS = ['0S', '0S', '2S', '2S']
Z_AXIS = [1.5637257394958113, 1.5399805470086181, 1.4030363999998978, 1.4198133749999822]
FREQ = [5000000.] * 4
xTickMarks = ["0B", "1B", "2B", "1B3S", "2B2S"]
yTickMarks = ["-1S","0S", "1S", "2S", "3S", "4S"]
matplotlib.rc('font', serif='Helvetica Neue')
matplotlib.rc('text', usetex='false')
matplotlib.rcParams.update({'font.size': 10})
fig = plt.figure(figsize=(11.69,4.88)) # for landscape
axes1 = fig.add_subplot(111)
'''
Tuple of big,small cores with a tuple of power.
let x be big core
let y be small core
let z be Power
'''
plt.grid(True,linestyle='-',color='0.75')
x = [xTickMarks.index(i) for i in X_AXIS]
y = [yTickMarks.index(i) for i in Y_AXIS]
z = Z_AXIS
s = [int(FREQ[i])/1000000.0 for i in range(len(FREQ))]
plt.scatter(x,y,s=s,c=z, marker = 'o', cmap = cm.jet )
cb = plt.colorbar()
cb.set_label('Frequency', fontsize=20)
axes1.set_xlim((0, len(xTickMarks)-1))
axes1.set_ylim((0, len(yTickMarks)-1))
axes1.set_xticks(xrange(len(xTickMarks)))
axes1.set_yticks(xrange(len(yTickMarks)))
axes1.set_xticklabels(xTickMarks)
axes1.set_yticklabels(yTickMarks)
axes1.set_ylabel('Small cores')
axes1.set_xlabel('Big cores')
axes1.legend(prop={'size':5}, ncol=4)
axes1.xaxis.grid(True)
figsize=(11.69,8.27) # for landscape
#fig.savefig('state-transition.png', bbox_inches='tight', dpi=300, pad_inches=0.1)
plt.show()
与您的代码相比,我在这里更改的是 x 和 y 的值:
x = [xTickMarks.index(i) for i in X_AXIS]
y = [yTickMarks.index(i) for i in Y_AXIS]
和你坐标轴的刻度:
axes1.set_xlim((0, len(xTickMarks)-1))
axes1.set_ylim((0, len(yTickMarks)-1))
axes1.set_xticks(xrange(len(xTickMarks)))
axes1.set_yticks(xrange(len(yTickMarks)))
axes1.set_xticklabels(xTickMarks)
axes1.set_yticklabels(yTickMarks)
希望这对您有所帮助。