Python + matplotlib: how can I change the line width of a line for one line?

I have a tablet consisting of 3 stacked series and 5 bars. I want to select one loaf (all three folded elements) by changing the line width.

I draw the columns with the following command:

mybar = ax.bar(x,Y[:,i],bottom=x,color=colors[i],edgecolor='none',width=wi,linewidth = 0) bar_handles = np.append(bar_handles,mybar) 

I have a handle to the bar that I want to change stored in the bar_handles array, is there a way to change the bar property of edgecolor and linewidth after drawing it?

+6
source share
2 answers

I ended up doing it like this:

 ax.axvspan(X1, X1+wi, ymax=Y2, facecolor='none', edgecolor='black', linewidth=2) 

... where

 X1 = bar_handles[startBlock].get_x() wi = bar_handles[startBlock].get_width() Y2 = ax.transLimits.transform((0,bar_handles[startBlock].get_height()))[1] 

This creates an edge above my panel - including all the elements inside - without the horizontal, as between the elements.

+3
source

ax.bar returns the Container performers; each "artist" is a Rectangle with the set_linewidth and set_edgecolor .

To change the settings of, say, the second column in mybar , you can do this:

 mybar[1].set_linewidth(4) mybar[1].set_edgecolor('r') 

Here's a script that shows how this can be used to change the width of the stack line:

 import numpy as np import matplotlib.pyplot as plt x = np.array([1,2,3]) y1 = np.array([3,2.5,1]) y2 = np.array([4,3,2]) y3 = np.array([1,4,1]) width = 0.5 handles = [] b1 = plt.bar(x, y1, color='#2040D0', width=width, linewidth=0) handles.append(b1) b2 = plt.bar(x, y2, bottom=y1, color='#60A0D0', width=width, linewidth=0) handles.append(b2) b3 = plt.bar(x, y3, bottom=y1+y2, color='#A0D0D0', width=width, linewidth=0) handles.append(b3) # Highlight the middle stack. for b in handles: b[1].set_linewidth(3) plt.xlim(x[0]-0.5*width, x[-1]+1.5*width) plt.xticks(x+0.5*width, ['A', 'B', 'C']) plt.show() 

This script creates the following histogram:

stacked bar chart

+9
source

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


All Articles