pandas 生成並排放置的條形圖和箱線圖


1、代碼

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# 生成數據,創建 DataFrame
np.random.seed(27)
data = np.random.rand(7, 3)
index = ['Customer ' + str(i) for i in range(1, 8)]
Metrics = ['Metric ' + str(i) for i in range(1, 4)]
df = pd.DataFrame(data, index=index, columns=pd.Index(Metrics, name='Metrics'))

# 設置圖形屬性及布局
plt.style.use('ggplot')
fig = plt.figure('百里希文')
axes = fig.subplots(nrows=1, ncols=2)
ax1, ax2 = axes.ravel()

# 在第 1 個坐標系創建豎直條形圖
df.plot(kind='bar', ax=ax1, alpha=0.7, title='Bar Plot' )
plt.setp(ax1.get_xticklabels(), rotation=45, fontsize=10)
plt.setp(ax1.get_yticklabels(), rotation=0, fontsize=10)
ax1.set_xlabel('Customer'), ax1.set_ylabel('Value')

# 在第 2 個坐標系創建箱線圖
colors = dict(boxes='DarkBlue', whiskers='Gray', medians='r', caps='k')
df.plot(kind='box', ax=ax2, color=colors, sym='r.', title='Box Plot')
plt.setp(ax2.get_xticklabels(), rotation=45, fontsize=11)
plt.setp(ax2.get_yticklabels(), rotation=0, fontsize=11)
ax2.set_xlabel('Metric'), ax2.set_ylabel('Value')

# 調整圖形顯示位置
fig.subplots_adjust(left=0.05, bottom=0.2, right=0.95,
                    top=0.95, hspace=0.1, wspace=0.1) 

plt.show()

2 圖形如下

 

 

。。。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM