1

I have a 'master' panda dataframe that has a time series of 'polarity' values for several terms. I want to work with 4 of them, so I extracted 4 separate dataframes, containing the time series(same time series for all of the terms, but different polarity values.)

I plotted them in 4 separate matplotlib graphs, using the code below

fig, axes = plt.subplots(nrows=2, ncols=2)
polarity_godzilla.plot(ax=axes[0,0]); axes[0,0].set_title('Godzilla')
polarity_henry_kissinger.plot(ax=axes[0,1]); axes[0,1].set_title('Henry Kissinger')
polarity_bmwi.plot(ax=axes[1,0]); axes[1,0].set_title('BMWi')
polarity_duran_duran.plot(ax=axes[1,1]); axes[1,1].set_title('Duran Duran')

Now, I want to graph them all in the same graph so I have an idea of the magnitude of each graph, because the auto scaling of matplotlib can give the wrong impression about the magnitude by just looking at the graphs. enter image description here

Two questions: 1) Is there are way to set the min and max values of the Y-axis when plotting? 2) I am not an expert in matplotlib, so I am not sure how to plot the 4 variables in the same graph using different colors, markers, labels, etc. I tried nrows = 1, ncols = 1 but can't plot anything.

Thank you

Luis Miguel
  • 5,057
  • 8
  • 42
  • 75

2 Answers2

1

axes[i,j].set_ylim([min,max], auto=False) will set the y-limits of the plot in the i,jth plot. auto=False keeps it from clobbering your settings.

You can plot multiple lines on the same graph by calling plt.hold(True), drawing a bunch of plots, and then calling plt.show() or plt.savefig(filename).

You can pass a color code into plt.plot() as a third positional argument. The syntax is a little byzantine (it's inherited from MATLAB); it's documented in the matplotlib.pyplot.plot documentation. You can pass this argument to DataFrame.plot as (for example) style='k--'.

For your case, I would try

fig, ax = plt.axes()
plt.hold(True)
polarity_godzilla.plot(ax=ax, style="k-o", label="Godzilla")
polarity_henry_kissinger(ax=ax, style="b-*", label="Kissinger")
#etc.
plt.legend()  #to draw a legend with the labels you provided
plt.show() #or plt.savefig(filename)
Nat Knight
  • 364
  • 2
  • 10
1

You can perhaps loop into your AxesSubplot objects and call autoscale passing the axis parameter:

for ax in axes:
    ax.autoscale(axis='y')
Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234