Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python绘图中添加新轴_Python_Pandas_Dataframe_Matplotlib - Fatal编程技术网

在python绘图中添加新轴

在python绘图中添加新轴,python,pandas,dataframe,matplotlib,Python,Pandas,Dataframe,Matplotlib,下面的数据框包含不同类型的数据 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=[

下面的数据框包含不同类型的数据

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')     

那么我想修改这个图如下:

  • 用我的数据框中的百分比和置信度替换勾号
  • 在左侧添加一个新的y轴,表示每个置信度的相应阈值
  • 在顶部添加一个新的x轴,表示每个百分比对应的大小

关键是复制轴,并固定原始轴和复制轴的轴限制,以便标记对齐

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()
结果:

您可以尝试:

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())

我的方法是复制现有轴,并给它们不同的刻度标签,即下面两列的刻度标签:

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()

到目前为止,您尝试过什么?(,)参见例如。