Matplotlib subheadings with the same settings

I draw the same data in two different formats: log scale and linear scale.

Basically, I want to have exactly the same plot, but with different scales, one on top of the other.

Now I have the following:

import matplotlib.pyplot as plt # These are the plot 'settings' plt.xlabel('Size') plt.ylabel('Time(s)'); plt.title('Matrix multiplication') plt.xticks(xl, rotation=30, size='small') plt.grid(True) # Settings are ignored when using two subplots plt.subplot(211) plt.plot(xl, serial_full, 'r--') plt.plot(xl, acc, 'bs') plt.plot(xl, cublas, 'g^') plt.subplot(212) plt.yscale('log') plt.plot(xl, serial_full, 'r--') plt.plot(xl, acc, 'bs') plt.plot(xl, cublas, 'g^') 

All "settings" before plt.subplot are ignored.

I can make it work the way I want, but I need to duplicate all the settings after each subheading declaration.

Is there a way to configure both subheadings at the same time?

+4
source share
2 answers

The plt.* Settings are usually applied to the current matplotlib plot; with plt.subplot , you start a new story, so the settings no longer apply to it. You can share shortcuts, ticks, etc., Axes objects related to charts ( see examples here ), but IMHO this would be superfluous here. Instead, I would suggest putting the general "style" in one function and invoking it for the plot:

 def applyPlotStyle(): plt.xlabel('Size') plt.ylabel('Time(s)'); plt.title('Matrix multiplication') plt.xticks(range(100), rotation=30, size='small') plt.grid(True) plt.subplot(211) applyPlotStyle() plt.plot(xl, serial_full, 'r--') plt.plot(xl, acc, 'bs') plt.plot(xl, cublas, 'g^') plt.subplot(212) applyPlotStyle() plt.yscale('log') plt.plot(xl, serial_full, 'r--') plt.plot(xl, acc, 'bs') plt.plot(xl, cublas, 'g^') 

On the side of the note, you can snatch more duplication by extracting your build commands into a function like this:

 def applyPlotStyle(): plt.xlabel('Size') plt.ylabel('Time(s)'); plt.title('Matrix multiplication') plt.xticks(range(100), rotation=30, size='small') plt.grid(True) def plotSeries(): applyPlotStyle() plt.plot(xl, serial_full, 'r--') plt.plot(xl, acc, 'bs') plt.plot(xl, cublas, 'g^') plt.subplot(211) plotSeries() plt.subplot(212) plt.yscale('log') plotSeries() 

On the other hand, it may be sufficient to put a title at the top of the picture (instead of each chart), for example, using suptitle . Similary, it may be enough for xlabel appear only under the second graph:

 def applyPlotStyle(): plt.ylabel('Time(s)'); plt.xticks(range(100), rotation=30, size='small') plt.grid(True) def plotSeries(): applyPlotStyle() plt.plot(xl, serial_full, 'r--') plt.plot(xl, acc, 'bs') plt.plot(xl, cublas, 'g^') plt.suptitle('Matrix multiplication') plt.subplot(211) plotSeries() plt.subplot(212) plt.yscale('log') plt.xlabel('Size') plotSeries() plt.show() 
+10
source

Hans response is probably recommended. But in case you still want to go on copying the axis properties to another axis, here is my method:

 fig = figure() ax1 = fig.add_subplot(2,1,1) ax1.plot([1,2,3],[4,5,6]) title('Test') xlabel('LabelX') ylabel('Labely') ax2 = fig.add_subplot(2,1,2) ax2.plot([4,5,6],[7,8,9]) for prop in ['title','xlabel','ylabel']: setp(ax2,prop,getp(ax1,prop)) show() fig.show() 

enter image description here

This allows you to set a whitelist for which the properties will be set. I currently have title , xlabel and ylabel , but you can just use getp(ax1) to print a list of all available properties.

You can copy all the properties using something like the following, but I recommend against it, as some properties parameters ruined the second plot. I tried using the blacklist to exclude some of them, but you will need to play with it to make it work:

 insp = matplotlib.artist.ArtistInspector(ax1) props = insp.properties() for key, value in props.iteritems(): if key not in ['position','yticklabels','xticklabels','subplotspec']: try: setp(ax2,key,value) except AttributeError: pass 

( except/pass - skip properties that are obtainable but not customizable)

+3
source

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


All Articles