国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python使用matplotlib繪圖時圖例顯示問題的解決

2019-11-25 16:13:30
字體:
供稿:網(wǎng)友

前言

matplotlib是基于Python語言的開源項目,旨在為Python提供一個數(shù)據(jù)繪圖包。在使用Python matplotlib庫繪制數(shù)據(jù)圖時,需要使用圖例標(biāo)注數(shù)據(jù)類別,但是傳參時,會出現(xiàn)圖例解釋文字只顯示第一個字符,需要在傳參時在參數(shù)后加一個逗號(應(yīng)該是python語法,加逗號,才可以把參數(shù)理解為元組類型吧),就可解決這個問題,

示例如下

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)計數(shù)據(jù)_每小時_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('時間索引') plt.ylabel('活動頻數(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)計數(shù)據(jù)_每小時_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('時間索引') plt.ylabel('活動頻數(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() 

顯示效果如下

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家學(xué)習(xí)或者使用python能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對武林網(wǎng)的支持。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 大兴区| 海盐县| 江油市| 永年县| 佛山市| 六枝特区| 宁武县| 新宾| 资源县| 沈阳市| 边坝县| 兴安县| 隆尧县| 来凤县| 通许县| 唐山市| 新宾| 蒙城县| 扎赉特旗| 海安县| 漯河市| 阆中市| 丽江市| 城口县| 遂溪县| 婺源县| 饶阳县| 汕头市| 左权县| 象州县| 渭源县| 永安市| 昂仁县| 洛扎县| 阿勒泰市| 龙南县| 南平市| 佳木斯市| 澳门| 孟村| 游戏|