我有以下数据框包含不同类型的数据
df = pandas.DataFrame(data=[
[20,69.262295,0.458615,244],
[40,52.049180,0.105605,488],
[60,37.380628,0.037798,733],
[80,28.659161,0.018166,977],
[100,23.013923,0.004136,1221]],
columns=['percentage','confidence','threshold','size'])
df
Out[121]:
percentage confidence threshold size
0 20 69.262295 0.458615 244
1 40 52.049180 0.105605 488
2 60 37.380628 0.037798 733
3 80 28.659161 0.018166 977
4 100 23.013923 0.004136 1221
首先,我想绘制百分比与置信度
fig = plt.figure()
plt.plot(df['percentage'],df['confidence'])
plt.ylabel('confidence')
plt.xlabel('percent of population')
然后我想修改这个数字如下:
答案 0 :(得分:0)
关键是要复制轴并修复原始轴和复制轴的轴限制,以使刻度线对齐。
fig, ax = plt.subplots(figsize=(16, 9))
ax.plot(df['percentage'],df['confidence'], marker='o')
ax.set_ylabel('confidence')
ax.set_xlabel('percent of population')
ax.set_xticks(df['percentage'])
ax.set_xticklabels(df['percentage'])
# Force the xaxis limits
ax.set_xlim(df['percentage'].min(), df['percentage'].max())
ax.set_yticks(df['confidence'])
ax.set_yticklabels(["{:.2f}".format(x) for x in df['confidence']])
ax.set_ylim(df['confidence'].min(), df['confidence'].max())
# Duplicate the xaxis, sharing the y one
ax2 = ax.twiny()
# We set the ticks location to 'percentage'
ax2.set_xticks(df['percentage'])
# But we annotate with 'size'
ax2.set_xticklabels(df['size'])
ax2.set_xlabel('size')
# Here too we fix the xaxis limits
ax2.set_xlim(df['percentage'].min(), df['percentage'].max())
# Same for the secondary Y axis
ax3 = ax.twinx()
ax3.set_yticks(df['confidence'])
ax3.set_yticklabels(["{:.2f}".format(x) for x in df['threshold']])
ax3.set_ylabel('threshold')
ax3.set_ylim(df['confidence'].min(), df['confidence'].max())
plt.show()
结果:
答案 1 :(得分:0)
您可以尝试:
x = df['percentage']
y = df['confidence']
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_ylabel('confidence')
ax1.plot(x, y)
ax2 = ax1.twinx()
ax3 = ax1.twiny()
ax2.set_ylabel('threshold')
ax2.set_ylim(df['threshold'].max(), df['threshold'].min())
ax3.set_xlabel('size')
ax3.set_xlim(df['size'].min(), df['size'].max())
答案 2 :(得分:0)
我的方法是复制现有的轴并给它们不同的刻度标签,即两个列的标签:
fig = plt.figure()
plt.plot(df['percentage'],df['confidence'])
plt.ylabel('confidence')
plt.xlabel('percent of population')
plt.xticks(df['percentage'])
plt.yticks(df['confidence'])
yt = plt.yticks()
yl = plt.ylim()
plt.twinx()
plt.yticks(yt[0], df['threshold'])
plt.ylim(yl)
plt.ylabel('threshold')
xt = plt.xticks()
xl = plt.xlim()
plt.twiny()
plt.xticks(xt[0], df['size'])
plt.xlim(xl)
plt.xlabel('size')
plt.tight_layout()