The realization of this form of graphics can be achieved by matplotlib and pandas, which is more convenient than the next pandas.
My data analysis is mainly stacked bar, bar and line in the form of a picture. Stacked bar is more complex if implemented with matplotlib (multiple groups)
Let's go.
defPlot_stacked_bar (Left_data, right_data): Width=. 3Axe= Plt.subplot (111) Axe= Left_data.plot (kind='Bar', Stacked=true, Ax=axe, Width=width, Use_index=true, legend=False) axe.set_xticklabels (left_data.index, rotation=0)#add patches to the stacked barPatterns = ('-','+','x','\\','*','o','O','.','/') Bars=Axe.patches Hatches="'. Join (H*len (Left_data) forHinchpatterns) forBar, HatchinchZip (bars, hatches): Bar.set_hatch (Hatch)#Plottint the line sharing the same x-axis on the secondary y-axisAXF =Axe.twinx () Axf.plot (Axe.get_xticks (), Right_data, LineStyle='-', marker='o', linewidth=2.0) Axf.set_ylim (0,90))
Another form of graphics:
fromMatplotlibImportPyplot as PltImportPandas as PD fromPandasImportSeriesImportNumPy as NPN= 50x= Pd.period_range ('2001-01-01', Periods=n, freq='M') Y1= (Series (NP.RANDOM.RANDN (n)) + 5). ToList () Y2=(Series (NP.RANDOM.RANDN (n))). ToList () DF= PD. DataFrame ({'Bar': Y2,' Line': y1}, index=x)#Let ' s plotPlt.figure (Figsize= (20, 4)) Ax1= df['Bar'].plot (kind='Bar', label='Bar') Ax2=ax1.twiny () df[' Line'].plot (kind=' Line', label=' Line', ax=ax2) Ax2.grid (color="Red", axis="x")defAlign_xaxis (ax2, AX1, X1, x2):"maps Xlim of AX2 to X1 and X2 in Ax1"(x1, _), (x2, _)=ax2.transData.inverted (). Transform (Ax1.transData.transform ([[x1, 0], [x2, 0]]) XS, xe=Ax2.get_xlim () K, B= Np.polyfit ([x1, X2], [XS, xe], 1) Ax2.set_xlim (XS*k+b, xe*k+b) Align_xaxis (AX2, ax1, 0, N-1)
#参考 #
- Http://stackoverflow.com/questions/19952290/how-to-align-the-bar-and-line-in-matplotlib-two-y-axes-chart? newreg=4f241248a9f44c34a467216c0a6cf099
- Http://stackoverflow.com/questions/22833404/how-do-i-plot-hatched-bars-using-pandas
- http://stackoverflow.com/questions/24227650/multiple-plots-on-same-figure-with-dataframe-plot/24237358#24237358
- Http://stackoverflow.com/questions/22623324/plot-bar-graph-and-timeseries-plot-on-different-axis-using-pandas
- Http://stackoverflow.com/questions/19060144/more-efficient-matplotlib-stacked-bar-chart-how-to-calculate-bottom-values
Two-figure drawing with the common y-axis