欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2022年01月20日 15:23:22   作者:哇咔君i  
python是一個很有趣的語言,可以在命令行窗口運行,下面這篇文章主要給大家介紹了關于python中如何利用matplotlib畫多個并列的柱狀圖的相關資料,需要的朋友可以參考下

首先如果柱狀圖中有中文,比如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  # 柱狀圖的寬度,可以根據(jù)自己的需求和審美來改

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. 要將柱狀圖的樣式畫成適合論文中使用的黑白并且?guī)Щy的樣式:

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()

以上

總結

到此這篇關于python中如何利用matplotlib畫多個并列柱狀圖的文章就介紹到這了,更多相關python matplotlib畫并列柱狀圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • python中的sys模塊詳解

    python中的sys模塊詳解

    sys模塊是與python解釋器交互的一個接口,sys 模塊提供了許多函數(shù)和變量來處理 Python 運行時環(huán)境的不同部分,這篇文章主要介紹了python之sys模塊詳解,需要的朋友可以參考下
    2022-11-11
  • pandas中聚合函數(shù)agg的具體用法

    pandas中聚合函數(shù)agg的具體用法

    Pandas中的的agg()函數(shù)為aggregate的縮寫.總數(shù)、合計、聚合的意思.是一個功能非常強大的函數(shù).在Pandas中可以利用agg()對Series、DataFrame以及groupby()后的結果進行聚合操作,下面這篇文章主要給大家介紹了關于pandas中聚合函數(shù)agg的具體用法,需要的朋友可以參考下
    2022-07-07
  • Python+request+unittest實現(xiàn)接口測試框架集成實例

    Python+request+unittest實現(xiàn)接口測試框架集成實例

    這篇文章主要介紹了Python+request+unittest實現(xiàn)接口測試框架集成實例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03
  • python做圖片搜索引擎并保存到本地詳情

    python做圖片搜索引擎并保存到本地詳情

    這篇文章主要介紹了python做圖片搜索引擎并保存到本地詳情,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下
    2022-08-08
  • python實現(xiàn)將range()函數(shù)生成的數(shù)字存儲在一個列表中

    python實現(xiàn)將range()函數(shù)生成的數(shù)字存儲在一個列表中

    這篇文章主要介紹了python實現(xiàn)將range()函數(shù)生成的數(shù)字存儲在一個列表中,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • python selenium禁止加載某些請求的實現(xiàn)

    python selenium禁止加載某些請求的實現(xiàn)

    本文主要介紹了python selenium禁止加載某些請求的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • python 爬蟲 實現(xiàn)增量去重和定時爬取實例

    python 爬蟲 實現(xiàn)增量去重和定時爬取實例

    今天小編就為大家分享一篇python 爬蟲 實現(xiàn)增量去重和定時爬取實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • Python的Flask框架中實現(xiàn)登錄用戶的個人資料和頭像的教程

    Python的Flask框架中實現(xiàn)登錄用戶的個人資料和頭像的教程

    這篇文章主要介紹了Python的Flask框架中實現(xiàn)登錄用戶的個人資料和頭像的教程,這也是各個web框架的最基本功能之一,需要的朋友可以參考下
    2015-04-04
  • python獲取網(wǎng)絡圖片方法及整理過程詳解

    python獲取網(wǎng)絡圖片方法及整理過程詳解

    這篇文章主要介紹了python獲取網(wǎng)絡圖片方法及整理過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12
  • python3.5實現(xiàn)socket通訊示例(TCP)

    python3.5實現(xiàn)socket通訊示例(TCP)

    本篇文章主要介紹了python3.5實現(xiàn)socket通訊示例(TCP),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02

最新評論