python使用matplotlib繪圖時(shí)圖例顯示問(wèn)題的解決
前言
matplotlib是基于Python語(yǔ)言的開源項(xiàng)目,旨在為Python提供一個(gè)數(shù)據(jù)繪圖包。在使用Python matplotlib庫(kù)繪制數(shù)據(jù)圖時(shí),需要使用圖例標(biāo)注數(shù)據(jù)類別,但是傳參時(shí),會(huì)出現(xiàn)圖例解釋文字只顯示第一個(gè)字符,需要在傳參時(shí)在參數(shù)后加一個(gè)逗號(hào)(應(yīng)該是python語(yǔ)法,加逗號(hào),才可以把參數(shù)理解為元組類型吧),就可解決這個(gè)問(wèn)題,
示例如下
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
from pylab import mpl
xmajorLocator = MultipleLocator(24 * 3) #將x軸主刻度標(biāo)簽設(shè)置為24 * 3的倍數(shù)
ymajorLocator = MultipleLocator(100 * 2) #將y軸主刻度標(biāo)簽設(shè)置為100 * 2的倍數(shù)
# 設(shè)置中文字體
mpl.rcParams['font.sans-serif'] = ['SimHei']
# 導(dǎo)入文件數(shù)據(jù)
data = np.loadtxt('H:/dataset/統(tǒng)計(jì)數(shù)據(jù)_每小時(shí)_ba.csv', delimiter=',', dtype=int)
# 截取數(shù)組數(shù)據(jù)
x = data[:, 0]
y = data[:, 1]
plt.figure(num=1, figsize=(8, 6))
ax = plt.subplot(111)
ax.xaxis.set_major_locator(xmajorLocator)
ax.yaxis.set_major_locator(ymajorLocator)
ax.xaxis.grid(True, which='major') #x坐標(biāo)軸的網(wǎng)格使用主刻度
ax.yaxis.grid(True, which='major') #x坐標(biāo)軸的網(wǎng)格使用主刻度
plt.xlabel('時(shí)間索引')
plt.ylabel('活動(dòng)頻數(shù)')
plt.title('折線圖')
plt.xlim(0, 1152)
plt.ylim(0, 2200)
#plt.plot(x, y, 'rs-')
line1 = ax.plot(x, y, 'b.-')
ax.legend(line1, ('微博'))
plt.show() 顯示效果如下

代碼修改
from pylab import mpl
xmajorLocator = MultipleLocator(24 * 3) #將x軸主刻度標(biāo)簽設(shè)置為24 * 3的倍數(shù)
ymajorLocator = MultipleLocator(100 * 2) #將y軸主刻度標(biāo)簽設(shè)置為100 * 2的倍數(shù)
# 設(shè)置中文字體
mpl.rcParams['font.sans-serif'] = ['SimHei']
# 導(dǎo)入文件數(shù)據(jù)
data = np.loadtxt('H:/dataset/統(tǒng)計(jì)數(shù)據(jù)_每小時(shí)_ba.csv', delimiter=',', dtype=int)
# 截取數(shù)組數(shù)據(jù)
x = data[:, 0]
y = data[:, 1]
plt.figure(num=1, figsize=(8, 6))
ax = plt.subplot(111)
ax.xaxis.set_major_locator(xmajorLocator)
ax.yaxis.set_major_locator(ymajorLocator)
ax.xaxis.grid(True, which='major') #x坐標(biāo)軸的網(wǎng)格使用主刻度
ax.yaxis.grid(True, which='major') #x坐標(biāo)軸的網(wǎng)格使用主刻度
plt.xlabel('時(shí)間索引')
plt.ylabel('活動(dòng)頻數(shù)')
plt.title('折線圖')
plt.xlim(0, 1152)
plt.ylim(0, 2200)
#plt.plot(x, y, 'rs-')
line1 = ax.plot(x, y, 'b.-')
ax.legend(line1, ('微博',)) # 多加一個(gè)逗號(hào)
plt.show() 顯示效果如下

總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家學(xué)習(xí)或者使用python能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Python 通過(guò)爬蟲實(shí)現(xiàn)GitHub網(wǎng)頁(yè)的模擬登錄的示例代碼
這篇文章主要介紹了Python 通過(guò)爬蟲實(shí)現(xiàn)GitHub網(wǎng)頁(yè)的模擬登錄的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Python爬蟲庫(kù)BeautifulSoup獲取對(duì)象(標(biāo)簽)名,屬性,內(nèi)容,注釋
如何利用Python爬蟲庫(kù)BeautifulSoup獲取對(duì)象(標(biāo)簽)名,屬性,內(nèi)容,注釋等操作下面就為大家介紹一下2020-01-01
python?泛型函數(shù)--singledispatch的使用解讀
這篇文章主要介紹了python?泛型函數(shù)--singledispatch的使用解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
利用python實(shí)現(xiàn)聚類分析K-means算法的詳細(xì)過(guò)程
K-means算法是很典型的基于距離的聚類算法,采用距離作為相似性的評(píng)價(jià)指標(biāo),即認(rèn)為兩個(gè)對(duì)象的距離越近,其相似度就越大,下面通過(guò)本文給大家介紹利用python實(shí)現(xiàn)聚類分析K-means算法的詳細(xì)過(guò)程,感興趣的朋友一起看看吧2021-11-11

