matplotlib画图——条形图

一.单条

import numpy as np
import matplotlib.pyplot as plt

N = 5
y1 = [20, 10, 30, 25, 15]
y2 = [15, 14, 34 ,10,5]
index = np.arange(5)

bar_width = 0.3
plt.bar(index , y1, width=0.3 , color=‘y‘)
plt.bar(index , y2, width=0.3 , color=‘b‘ ,bottom=y1)
plt.show()

二.误差棒

mean_values = [1,2,3]
#误差范围
variance = [0.2,0.4,0.5]
bar_label = [‘bar1‘,‘bar2‘,‘bar3‘]

x_pos = list(range(len(bar_label)))
plt.bar(x_pos,mean_values,yerr=variance,alpha=0.7)
max_y = max(zip(mean_values,variance))
plt.ylim([0,max_y[0]+max_y[1]*1.2])
plt.ylabel(‘variable y‘)
plt.xticks(x_pos,bar_label)
plt.show()

三.背靠背

x1 = np.array([1,2,3])
x2 = np.array([2,2,3])

bar_labels = [‘bar1‘,‘bar2‘,‘bar3‘]
fig = plt.figure(figsize=(8,6))
y_pos = np.arange(len(x1))
y_pos = [x for x in y_pos]
#bar竖着 barh横着
plt.barh(y_pos,x1,color=‘g‘,alpha=0.5)
plt.barh(y_pos,-x1,color=‘b‘,alpha=0.5)
#x y轴范围限制
plt.xlim(-max(x2)-1,max(x1)+1)
plt.ylim(-1,len(x1)+1)
plt.show()

四.三条

green_data = [1,2,3]
blue_data = [3,2,1]
red_data = [2,3,1]
labels = [‘group 1‘,‘group 2‘,‘group 3‘]

pos = list(range(len(green_data)))
width = 0.2
fig,ax = plt.subplots(figsize=(8,6))

plt.bar(pos, green_data,width,alpha=0.5,color=‘g‘,label=labels[0])
plt.bar([p+width for p in pos], green_data,width,alpha=0.5,color=‘b‘,label=labels[1])
plt.bar([p+width*2 for p in pos], green_data,width,alpha=0.5,color=‘r‘,label=labels[2])
plt.show()

五.正负

x = np.arange(5)
#(-5,5)随机五个数
y = np.random.randint(-5,5,5)
fig,ax = plt.subplots()
v_bars = ax.bar(x,y,color=‘lightblue‘)
for bar,height in zip(v_bars,y):
    if height < 0:
        bar.set(edgecolor = ‘darkred‘, color = ‘green‘, linewidth = 3)

六.标线

#随机五个数
data = range(200,225,5)
#坐标标注
bar_labels = [‘a‘,‘b‘,‘c‘,‘d‘,‘e‘]
#条形的长宽
fig = plt.figure(figsize=(10,8))
#5个
y_pos = np.arange(len(data))
plt.yticks(y_pos, bar_labels, fontsize=16)
bars = plt.barh(y_pos,data,alpha = 0.5,color = ‘g‘)
#按照最小值的位置画垂直的竖线
plt.vlines(min(data), -1, len(data)+0.5,linestyles=‘dashed‘)
#把值写到后面
for b,d in zip(bars,data):
    plt.text(b.get_width() + b.get_width()*0.05,
             b.get_y()+b.get_height()/2,
             ‘{0:.2%}‘.format(d/min(data)))
plt.show()

另:折线填充

x = np.random.randn(100).cumsum()
y = np.linspace(0,10,100)

fig,ax = plt.subplots()
#折线图填充
ax.fill_between(x,y,color=‘lightblue‘)

x = np.linspace(0,10,200)
y1 = 2*x + 1
y2 = 3*x +1.2
y_mean = 0.5*x*np.cos(2*x) + 2.5*x + 1.1
fig,ax = plt.subplots()
ax.fill_between(x,y1,y2,color=‘red‘)
ax.plot(x,y_mean,color=‘black‘)

原文地址:https://www.cnblogs.com/lingluo2017/p/9484568.html

时间: 2024-07-31 13:07:58

matplotlib画图——条形图的相关文章

matplotlib 画图 条形图

#绘制条形图 import numpy as np import matplotlib.pyplot as plt y=[] plt.figure(1) width=1 for i in range(len(y)): plt.figure(1) plt.bar(i*width,y[i],width) plt.xlabel("X") plt.ylabel("Y") plt.show() """ bar后面的参数,第一个是设置每个柱子左边缘

用python matplotlib 画图

state-machine environment object-oriente interface figure and axes backend and frontend user interface bankends hardcopy backends or non-interactive backends confugure your backends renderer : AGG import matplotlib.pyplot as plt plt.plot() 可一次画好几个, r

ArcGIS10中matplotlib画图时的中文设置

利用GIS的数据批量生成XY的图形图像文件,可以直接使用Python.一般大家都是用matplotlib,中文设置的问题参看了许多内容,结论是对错不一,让我折腾了三天,现总结如下: 1.软件的版本.安装测试的为numpy-1.6.1和matplotlib-1.1.0,WindowsXP系统.我原来系统安装的numpy-1.6和matplotlib-1.1.0有冲突. 2.修改matplotlibrc文件.ArcGIS10下安装后,该文件在C:\Python26\ArcGIS10.0\Lib\si

python3 使用matplotlib画图出现中文乱码的情况

python3使用matplotlib画图,因python3默认使用中unicode编码,所以在写代码时不再需要写 plt.xlabel(u'人数'),而是直接写plt.xlabel('人数'). 注意: 在有中文的地方加上中文相关的字体,不然会因为没有字体显示成放框,因为默认的使用的字体里没有中文的,使用例子如下: # -*- coding: utf-8 -*- import pandas as pd import numpy as np from pandas import Series,D

matplotlib画图报错This figure includes Axes that are not compatible with tight_layout, so results might be incorrect.

之前用以下代码将实验结果用matplotlib show出来 plt.plot(np.arange(len(aver_reward_list)), aver_reward_list) plt.ylabel('Average Reward') plt.xlabel('Episode') plt.tight_layout() plt.savefig("AverageReward.eps") plt.show() 画出的图都没什么问题,但忽然有一次数据量比较大,matplotlib开始报错,

matplotlib画图出现乱码情况

python3使用matplotlib画图,因python3默认使用中unicode编码,所以在写代码时不再需要写 plt.xlabel(u’人数’),而是直接写plt.xlabel(‘人数’). 注意: 在有中文的地方加上中文相关的字体,不然会因为没有字体显示成放框,因为默认的使用的字体里没有中文的,使用例子如下: # -*- coding: utf-8 -*- import pandas as pd import numpy as np from pandas import Series,D

Anaconda3使用matplotlib画图报错问题的解决方法

在Anaconda3中使用matplotlib画图报错:ModuleNotFoundError: No module named 'PyQt4' 试了网上的几种方法都不奏效,最后在http://www.lfd.uci.edu/~gohlke/pythonlibs/#pyqt4 上下载和Python版本对应PyQt4的.whl文件,然后用pip命令安装. 此刻再执行程序不报上述错误,在执行到plt.figure()命令时又报:TypeError:'figure' is an unkown keyw

读书笔记5基于matplotlib画图

一.导入需要的模块 import numpy as np import matplotlib.pyplot as plt import seaborn as sns import scipy.stats as stats 二.画基本图形 1.plot画图 y=np.random.randn(100) plt.plot(y,'b-') plt.xlabel('x') plt.ylabel('y') plt.title(u'title') plt.show() 可选参数如下所示: 也可以通过更改参数

使用Matplotlib画图系列(一)

实现一个最简单的plot函数调用: 1 import matplotlib.pyplot as plt 2 3 y=pp.DS.Transac_open # 设置y轴数据,以数组形式提供 4 5 x=len(y) # 设置x轴,以y轴数组长度为宽度 6 x=range(x) # 以0开始的递增序列作为x轴数据 7 8 plt.plot(x,y) # 只提供x轴,y轴参数,画最简单图形 图形输出结果类似于: 加入新方法: plt.figure() :自定义画布大小 plt.subplot() :设