日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

python中如何利用matplotlib畫多個并列的柱狀圖_python

作者:哇咔君i ? 更新時間: 2022-04-03 編程語言

首先如果柱狀圖中有中文,比如X軸和Y軸標簽需要寫中文,解決中文無法識別和亂碼的情況,加下面這行代碼就可以解決了:

plt.rcParams['font.sans-serif'] = ['SimHei']   # 解決中文亂碼

以下總共展示了三種畫不同需求的柱狀圖:

畫多組兩個并列的柱狀圖:

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

plt.rcParams['font.sans-serif'] = ['SimHei']

labels = ['G1', 'G2', 'G3', 'G4', 'G5']
men_means = [20, 34, 30, 35, 27]
women_means = [25, 32, 34, 20, 25]

x = np.arange(len(labels))  # the label locations
width = 0.35  # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, men_means, width, label='Men')
rects2 = ax.bar(x + width/2, women_means, width, label='Women')

# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()


def autolabel(rects):
    """Attach a text label above each bar in *rects*, displaying its height."""
    for rect in rects:
        height = rect.get_height()
        ax.annotate('{}'.format(height),
                    xy=(rect.get_x() + rect.get_width() / 2, height),
                    xytext=(0, 3),  # 3 points vertical offset
                    textcoords="offset points",
                    ha='center', va='bottom')


autolabel(rects1)
autolabel(rects2)

fig.tight_layout()

plt.show()

繪制好的柱狀圖如下:

畫兩組5個并列的柱狀圖:

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

plt.rcParams['font.sans-serif']=['SimHei'] # 解決中文亂碼

labels = ['第一項', '第二項']
a = [4.0, 3.8]
b = [26.9, 48.1]
c = [55.6, 63.0]
d = [59.3, 81.5]
e = [89, 90]    


x = np.arange(len(labels))  # 標簽位置
width = 0.1  # 柱狀圖的寬度,可以根據自己的需求和審美來改

fig, ax = plt.subplots()
rects1 = ax.bar(x - width*2, a, width, label='a')
rects2 = ax.bar(x - width+0.01, b, width, label='b')
rects3 = ax.bar(x + 0.02, c, width, label='c')
rects4 = ax.bar(x + width+ 0.03, d, width, label='d')
rects5 = ax.bar(x + width*2 + 0.04, e, width, label='e')


# 為y軸、標題和x軸等添加一些文本。
ax.set_ylabel('Y軸', fontsize=16)
ax.set_xlabel('X軸', fontsize=16)
ax.set_title('這里是標題')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()


def autolabel(rects):
    """在*rects*中的每個柱狀條上方附加一個文本標簽,顯示其高度"""
    for rect in rects:
        height = rect.get_height()
        ax.annotate('{}'.format(height),
                    xy=(rect.get_x() + rect.get_width() / 2, height),
                    xytext=(0, 3),  # 3點垂直偏移
                    textcoords="offset points",
                    ha='center', va='bottom')

autolabel(rects1)
autolabel(rects2)
autolabel(rects3)
autolabel(rects4)
autolabel(rects5)

fig.tight_layout()

plt.show()

繪制好的柱狀圖如下:

3. 要將柱狀圖的樣式畫成適合論文中使用的黑白并且帶花紋的樣式:

import matplotlib.pyplot as plt
import numpy as np

plt.rcParams['font.sans-serif'] = ['SimHei']  # 解決中文亂碼

labels = ['第一項', '第二項']
a = [50, 80]
b = [37, 69]
c = [78, 60]
d = [66, 86]
e = [80, 95]
# marks = ["o", "X", "+", "*", "O"]

x = np.arange(len(labels))  # 標簽位置
width = 0.1  # 柱狀圖的寬度

fig, ax = plt.subplots()
rects1 = ax.bar(x - width * 2, a, width, label='a', hatch="...", color='w', edgecolor="k")
rects2 = ax.bar(x - width + 0.01, b, width, label='b', hatch="oo", color='w', edgecolor="k")
rects3 = ax.bar(x + 0.02, c, width, label='c', hatch="++", color='w', edgecolor="k")
rects4 = ax.bar(x + width + 0.03, d, width, label='d', hatch="XX", color='w', edgecolor="k")
rects5 = ax.bar(x + width * 2 + 0.04, e, width, label='e', hatch="**", color='w', edgecolor="k")

# 為y軸、標題和x軸等添加一些文本。
ax.set_ylabel('Y', fontsize=16)
ax.set_xlabel('X', fontsize=16)
ax.set_title('標題')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()

以上

總結

原文鏈接:https://blog.csdn.net/weixin_44293949/article/details/114590319

欄目分類
最近更新