Auto-scaling in matplotlib, plotting different time series on one chart

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

I built them on 4 separate matplotlib plots 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 all of them to be on the same graph, so I have an idea of ​​the magnitude of each graph, because matplotlib auto-scaling can give a wrong idea of ​​the magnitude just by looking at the graphs. enter image description here

Two questions: 1) Is there a way to set the minimum and maximum values ​​of the Y axis when plotting? 2) I'm not an expert in matplotlib, so I'm not sure how to build 4 variables on the same chart using different colors, markers, labels, etc. I tried nrows = 1, ncols = 1, but can't build anything.

thanks

+1
source share
2 answers

axes[i,j].set_ylim([min,max], auto=False) sets the y-limits of the graph in the graph i,j th. auto=False does not allow you to reset your settings.

You can build multiple lines on one chart by calling plt.hold(True) , drawing a bunch of charts, and then calling plt.show() or plt.savefig(filename) .

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

In your case, I would try

 fig, ax = plt.axes() plt.hold(True) polarity_godzilla.plot(ax=ax, style="ko", 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) 
+1
source

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

 for ax in axes: ax.autoscale(axis='y') 
+1
source

Source: https://habr.com/ru/post/989217/


All Articles