python - Matplotlib: get colors and x/y data from a bar plot -
i have bar plot , want colors , x/y values. here sample code:
import matplotlib.pyplot plt def main(): x_values = [1,2,3,4,5] y_values_1 = [1,2,3,4,5] y_values_2 = [2,4,6,8,10] f, ax = plt.subplots(1,1) ax.bar(x_values,y_values_2,color='r') ax.bar(x_values,y_values_1,color='b') #any methods? plt.show() if __name__ == '__main__': main()
are there methods like ax.get_xvalues()
, ax.get_yvalues()
, ax.get_colors()
, can use extract ax
lists x_values
, y_values_1
, y_values_2
, colors 'r'
, 'b'
?
the ax
knows geometric objects it's drawing, nothing keeps track of when geometric objects added, , of course doesn't know "mean": patch comes bar-plot, etc. coder needs keep track of re-extract right parts further use. way common many python programs: call barplot
returns barcontainer
, can name @ time , use later:
import matplotlib.pyplot plt def main(): x_values = [1,2,3,4,5] y_values_1 = [1,2,3,4,5] y_values_2 = [2,4,6,8,10] f, ax = plt.subplots(1,1) rbar = ax.bar(x_values,y_values_2,color='r') bbar = ax.bar(x_values,y_values_1,color='b') return rbar, bbar if __name__ == '__main__': rbar, bbar = main() # stuff barplot data: assert(rbar.patches[0].get_facecolor()==(1.0,0.,0.,1.)) assert(rbar.patches[0].get_height()==2)
Comments
Post a Comment