1

I'm plotting out a figure where I want to lay several lines plotted out from multiple DataFrames into the same x-y space. I had managed to achieve this with the following code:

fig = plt.figure()
ax = fig.add_subplot(1,1,1)

ax.plot(simulation_cycles_geomean_df1, 'g', label = 'Scheduler = SDC, Max Chain Delay = 0.9')    # We can feed a DataFrame object as input directly to matplotlib since DataFrame is based on Numpy array object
ax.plot(simulation_cycles_geomean_df2, 'r', label = 'Scheduler = SDC, Max Chain Delay = 1000.9')
ax.plot(simulation_cycles_geomean_df3, 'b', label = 'Scheduler = List, Max Chain Delay = 0.9')
ax.plot(simulation_cycles_geomean_df4, 'y', label = 'Scheduler = List, Max Chain Delay = 1000.9')

ax.legend(loc = 'best')
fig.savefig('combined_plot.pdf')

where one of the DataFrame objects, say simulation_cycles_geomean_df1, looks like:

In [76]: data_analysis.simulation_cycles_geomean_df1 # data_analysis is the module that contains the source code
Out[76]: 
                    cycles
build_number              
1300           4932.788490
1301           4967.047532
1302           4968.503010
1303           4966.810638
1304           4964.470973
...
...
...
# And so on... until 1344

I am able to have the following figure using the above plotting statments (the breaks in the lines are ok since I have NaN values in my DataFrame):

enter image description here

But as you can see in the x-axis, the values do not exactly go by 1300, 1301, 1302 ... 1344, but start from 0 instead. Does anyone have any idea how I could set Matplotlib to have the x-axis follow the build_number index of my DataFrame exactly?

Another question I would like to ask is, looking at the figure, you can see that the legend is inevitably big, and it covers some of the graphs. Does anyone know how I could move the legend out somewhere, or anything that I could do, so that it does not cover any of the lines?

Thank you very much.

AKKO
  • 973
  • 2
  • 10
  • 18
  • 3
    You should be able to use `simulation_cycles_geomean_df1.plot(ax=ax)` and it will work. Otherwise `ax.plot(simulation_cycles_geomean_df1.index, simulation_cycles_geomean_df1)` should do it. – TomAugspurger Jan 22 '15 at 13:50
  • `ax.plot(simulation_cycles_geomean_df1.index, simulation_cycles_geomean_df1)` worked very well. Thanks @TomAugspurger ! Apart from this question, do you know how I can position the legend such that it does not cover any of the lines? – AKKO Jan 26 '15 at 02:24
  • 1
    http://stackoverflow.com/a/4701285/1889400 – TomAugspurger Jan 26 '15 at 14:59

1 Answers1

0

Here is an example with a dummy_data dataframe. The data looks like:

my_index    count
49         9320.0
50         9418.0
51        10490.0
52         7376.0
53         5018.0

First of all, make sure the labels you want on your x-axis is index of your dataframe. Then, use plot() function of pandas dataframe and it works like charm.

dummy_data.plot()

The plot looks like this now: enter image description here

Regarding your second answer, you can move the legend using plt.legend().

dummy_data.plot(legend=True)
plt.legend(loc='upper left', bbox_to_anchor=(1, 0.5),fancybox = True)

The plot looks like this now: enter image description here

Other valid "loc" options are: [upper center, center, lower left, center left, right, center right, upper right, lower right, upper left, lower center, best]. Try and see which one works best for you.