Detailed explanation of DataFrame.plot function (3)

Detailed explanation of DataFrame.plot function (3)

The third part mainly introduces the use of the two functions df.bar and df.barh, as well as the effects of the three parameters rot, alpha, and stacked.

1. bar

speed = [0.1, 17.5, 40, 48, 52, 69, 88]
lifespan = [2, 8, 70, 1.5, 25, 12, 28]
index = ['snail', 'pig', 'elephant',
         'rabbit', 'giraffe', 'coyote', 'horse']
df = pd.DataFrame({'speed': speed,
                   'lifespan': lifespan}, index=index)
df.plot.bar(rot=-30,alpha=0.5)  
plt.show()

rot=-30 X-axis label rotates 30 degrees to the right
alpha=0.5 transparency 50%

The effect is as follows:
Insert image description here

speed = [0.1, 17.5, 40, 48, 52, 69, 88]
lifespan = [2, 8, 70, 1.5, 25, 12, 28]
index = ['snail', 'pig', 'elephant',
         'rabbit', 'giraffe', 'coyote', 'horse']
df = pd.DataFrame({'speed': speed,
                   'lifespan': lifespan}, index=index)
df.plot.bar(rot=-30,alpha=0.5,stacked=True)  
plt.show()

stacked=True, column chart stacked display

Insert image description here

df = pd.DataFrame(abs(np.random.randn(8,2)), columns=['A','B'])
axes = df.plot.bar(rot=0, subplots=True, alpha=0.6)
axes[0].legend(loc='upper left')  
axes[1].legend(loc='upper right')  
plt.show()

axes[0].legend df.plot return value is an axes object, and set the icon position of the object respectively.

Insert image description here

2.barh

index = ['snail', 'pig', 'elephant','rabbit', 'giraffe', 'coyote', 'horse', 'cock']

df = pd.DataFrame(abs(np.random.randn(8,2)), columns=['A','B'],index=index)
ax = df.plot.barh(alpha=0.6)

Simple horizontal bar chart
Insert image description here

ax = df.plot.barh(alpha=0.6,stacked=True,color={"A": "red", "B": "green"})

color={“A”: “red”, “B”: “green”} sets different colors for the two columns of data respectively.

Horizontal stacked column chart
Insert image description here
previous article Line function, and the main parameters style, marker, color, linewidth, markersize, grid, xlim, ylim, loc, subplot use the next article
area, pie, hist, hexbin function demonstration

Guess you like

Origin blog.csdn.net/qq_39065491/article/details/132472218