提问者:小点点

使用DataFrame绘制双轴图


我有一个DataFrame,看起来像这样:

                state   runtime   pixels  segments
0                 Texas  0.079277  1756374     12960
1            California  0.045553  1221211      5129
2          Rhode Island  0.002466     8134      1247
3            Washington  0.016046   339786      6854
4               Alabama  0.009114   214936      1930
5  District of Columbia  0.000799      506       218
6                  Ohio  0.007617   192800      2949

我正试图沿着共享的x轴用双y轴绘制此数据帧(运行时)

我已使用以下代码完成此操作:

import pandas as pd
import matplotlib.pyplot as plt
from pylab import figure, show, legend, ylabel

data = pd.read_excel('runtimes.xlsx')

## create the general figure
fig1 = figure()

ax1 = fig1.add_subplot(111)
ax1.plot(data[['runtime', 'pixels']].T)

ax1.set_ylabel('Pixels')
ax1.set_xlabel('Runtime (s)')

ax2 = ax1.twinx()

ax2.plot(data[['runtime', 'segments']].T)
ax2.set_ylabel('Segments', color='r')
for tl in ax2.get_yticklabels():
    tl.set_color('r')

我正试图解决两个问题:(1)当我只想让每行打印一次时,它会将每行打印两次-我如何解决这个问题?(2) 如何添加图例,以便您可以知道哪一行指示其正确状态?


共1个答案

匿名用户

我发现,对于这类情况,通常更容易明确列,而不是让熊猫自动完成所有操作。例如。

ax1.scatter(data['runtime'], data['pixels'])

ax2.scatter(data['runtime'], data['segments'])

有关演示这一点的完整示例:

import pandas as pd
import matplotlib.pyplot as plt
from pylab import figure, show, legend, ylabel

data = pd.DataFrame({'runtime': [0.079277, 0.045553, 0.002466, 0.016046, 0.009114,
                                 0.000799, 0.007617],
                     'pixels':  [1756374, 1221211, 8134, 339786, 214936, 506, 192800],
                     'segments':[12960, 5129, 1247, 6854, 1930, 218, 2949]})

## create the general figure
fig1 = figure()

ax1 = fig1.add_subplot(111)
ax1.scatter(data['runtime'], data['pixels'], label="Pixels", marker='.', color='k')

ax1.set_ylabel('Pixels')
ax1.set_xlabel('Runtime (s)')

ax2 = ax1.twinx()

ax2.scatter(data['runtime'], data['segments'], label="Segments", marker='.', color='r')
ax2.set_ylabel('Segments', color='r')
for tl in ax2.get_yticklabels():
    tl.set_color('r')
fig1.legend(bbox_to_anchor=(0.225,0.845))
plt.show()

您还可以注意到图例,您可以通过更改bbox_to_anchor元组来更改位置

编辑

如果您需要基于状态设置颜色,那么您可以这样做

import pandas as pd
import matplotlib.pyplot as plt
from pylab import figure, show, legend, ylabel
import matplotlib.lines as mlines

data = pd.DataFrame({'state':   ["Texas", "California", "Rhode Island", "Washington", 
                                 "Alabama", "District of Columbia", "Ohio"],
                     'runtime': [0.079277, 0.045553, 0.002466, 0.016046, 
                                 0.009114, 0.000799, 0.007617],
                     'pixels':  [1756374, 1221211, 8134, 339786, 214936, 506, 192800],
                     'segments':[12960, 5129, 1247, 6854, 1930, 218, 2949]})

## create the general figure
fig1 = figure()

ax1 = fig1.add_subplot(111)
ax2 = ax1.twinx()
for ii in range(len(data['state'])):
    ax1.scatter(data['runtime'][ii], data['pixels'][ii], 
                label=data['state'][ii], marker='.')
    ax2.scatter(data['runtime'][ii], data['segments'][ii], marker='+')
ax1.set_ylabel('Pixels')
ax1.set_xlabel('Runtime (s)')
legend = fig1.legend(bbox_to_anchor=(0.3,0.845))
m1 = mlines.Line2D([], [], color='black', linewidth = 0, marker='.', label='Pixels')
m2 = mlines.Line2D([], [], color='black', linewidth = 0, marker='+', label='Segments')
plt.legend(handles=[m1,m2], loc='lower right')


ax2.set_ylabel('Segments', color='r')
for tl in ax2.get_yticklabels():
    tl.set_color('r')
plt.show()