Python 官方文档:入门教程 => 点击学习
目录柱状图水平绘制柱状图多个柱状图叠加型柱状图散点图气泡图直方图箱线图添加文字描述添加文字描述 方法二多个图形描绘 subplots使用pandas 绘图Matplotlib官网&n
Matplotlib官网 如果想了解更多可查看官网。
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline #写了这个就可以不用写plt.show()
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
X = np.linspace(0, 2*np.pi,100)# 均匀的划分数据
Y = np.sin(X)
Y1 = np.cos(X)
plt.title("Hello World!!")
plt.plot(X,Y)
plt.plot(X,Y1)
X = np.linspace(0, 2*np.pi,100)
Y = np.sin(X)
Y1 = np.cos(X)
plt.subplot(211) # 等价于 subplot(2,1,1) #一个图版画两个图
plt.plot(X,Y)
plt.subplot(212)
plt.plot(X,Y1,color = 'r')
data = [5,25,50,20]
plt.bar(range(len(data)),data)
data = [5,25,50,20]
plt.barh(range(len(data)),data)
data = [[5,25,50,20],
[4,23,51,17],
[6,22,52,19]]
X = np.arange(4)
plt.bar(X + 0.00, data[0], color = 'b', width = 0.25,label = "A")
plt.bar(X + 0.25, data[1], color = 'g', width = 0.25,label = "B")
plt.bar(X + 0.50, data[2], color = 'r', width = 0.25,label = "C")
# 显示上面设置的 lable
plt.legend()
data = [[5,25,50,20],
[4,23,51,17],
[6,22,52,19]]
X = np.arange(4)
plt.bar(X, data[0], color = 'b', width = 0.25)
plt.bar(X, data[1], color = 'g', width = 0.25,bottom = data[0])
plt.bar(X, data[2], color = 'r', width = 0.25,bottom = np.array(data[0]) + np.array(data[1]))
plt.show()
N = 50
x = np.random.rand(N)
y = np.random.rand(N)
plt.scatter(x, y)
N = 50
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.randn(N) # 颜色可以用数值表示
area = np.pi * (15 * np.random.rand(N))**2 # 调整大小
plt.scatter(x, y, c=colors, alpha=0.5, s = area)
N = 50
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.randint(0,2,size =50)
plt.scatter(x, y, c=colors, alpha=0.5,s = area)
a = np.random.rand(100)
plt.hist(a,bins= 20)
plt.ylim(0,15)
a = np.random.randn(10000)
plt.hist(a,bins=50)
plt.title("标准正太分布")
x = np.random.randint(20,100,size = (30,3))
plt.boxplot(x)
plt.ylim(0,120)
# 在x轴的什么位置填一个 label,我们这里制定在 1,2,3 位置,写上 A,B,C
plt.xticks([1,2,3],['A','B','C'])
plt.hlines(y = np.median(x,axis = 0)[0] ,xmin =0,xmax=3)
# 设置画布颜色为 blue
fig, ax = plt.subplots(facecolor='blue')
# y 轴数据
data = [[5,25,50,20],
[4,23,51,17],
[6,22,52,19]]
X = np.arange(4)
plt.bar(X+0.00, data[0], color = 'darkorange', width = 0.25,label = 'A')
plt.bar(X+0.25, data[1], color = 'steelblue', width = 0.25,label="B")
plt.bar(X+0.50, data[2], color = 'violet', width = 0.25,label = 'C')
ax.set_title("Figure 2")
plt.legend()
# 添加文字描述 方法一
W = [0.00,0.25,0.50]
for i in range(3):
for a,b in zip(X+W[i],data[i]):
plt.text(a,b,"%.0f"% b,ha="center",va= "bottom")
plt.xlabel("Group")
plt.ylabel("Num")
plt.text(0.0,48,"TEXT")
X = np.linspace(0, 2*np.pi,100)# 均匀的划分数据
Y = np.sin(X)
Y1 = np.cos(X)
plt.plot(X,Y)
plt.plot(X,Y1)
plt.annotate('Points',
xy=(1, np.sin(1)),
xytext=(2, 0.5), fontsize=16,
arrowprops=dict(arrowstyle="->"))
plt.title("这是一副测试图!")
%pylab inline
pylab.rcParams['figure.figsize'] = (10, 6) # 调整图片大小
# np.random.seed(19680801)
n_bins = 10
x = np.random.randn(1000, 3)
fig, axes = plt.subplots(nrows=2, ncols=2)
ax0, ax1, ax2, ax3 = axes.flatten()
colors = ['red', 'tan', 'lime']
ax0.hist(x, n_bins, nORMed=1, histtype='bar', color=colors, label=colors)
ax0.legend(prop={'size': 10})
ax0.set_title('bars with legend')
ax1.hist(x, n_bins, normed=1, histtype='bar', stacked=True)
ax1.set_title('stacked bar')
ax2.hist(x, n_bins, histtype='step', stacked=True, fill=False)
ax2.set_title('stack step (unfilled)')
# Make a multiple-histogram of data-sets with different length.
x_multi = [np.random.randn(n) for n in [10000, 5000, 2000]]
ax3.hist(x_multi, n_bins, histtype='bar')
ax3.set_title('different sample sizes')
import pandas as pd
df = pd.DataFrame(np.random.rand(50, 2), columns=['a', 'b'])
# 散点图
df.plot.scatter(x='a', y='b')
df = pd.DataFrame(np.random.rand(10,4),columns=['a','b','c','d'])
# 绘制柱状图
df.plot.bar()
# 堆积的柱状图
df.plot.bar(stacked=True)
# 水平的柱状图
df.plot.barh(stacked=True)
df = pd.DataFrame({'a':np.random.randn(1000)+1,'b':np.random.randn(1000),'c':np.random.randn(1000) - 1}, columns=['a', 'b', 'c'])
# 直方图
df.plot.hist(bins=20)
# 箱线图
df = pd.DataFrame(np.random.rand(10, 5), columns=['A', 'B', 'C', 'D', 'E'])
df.plot.box()
到此这篇关于python使用Matplotlib绘制多种常见图形的文章就介绍到这了,更多相关Matplotlib绘制图图形内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: python使用Matplotlib绘制多种常见图形
本文链接: https://lsjlt.com/news/118109.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0