欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Matplotlib - 绘图

程序员文章站 2022-03-24 15:46:43
...

折线图

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

plt.rcParams[‘font.sans-serif’] = [‘SimHei’]

dict = {“x”: [0, 1, 2, 3, 4, 5, 6], “y”: [900, 400, 200, 300, 500, 600, 800], “z”:[600, 150, 100, 180, 200, 250, 700]}
df = pd.DataFrame(dict)

plt.figure(figsize=(20, 20), dpi=80) # 设置画布大小,清晰度

plt.plot(df.x, df.y, color=‘b’)

plt.plot(df.x, df.z, color=‘g’)

dict1 = {0: ‘日’, 1: ‘一’, 2: ‘二’, 3: ‘三’, 4: ‘四’, 5: ‘五’, 6: ‘六’}

plt.xticks(df.x, [“星期%s” % dict1[i] for i in df.x], fontsize=20) # x轴刻度

plt.xlabel(“周日期”, fontsize=20) # x轴名称
plt.ylabel(“到店人数”, fontsize=20) # y轴名称
plt.title(“周到店人数统计”, fontsize=20) # 图标题

plt.legend([“到店人数”, “消费人数”], title=“到店消费比”, fontsize=‘x-large’)

plt.show()

饼图

mark = [‘America’, ‘China’, ‘India’, ‘Laos’, ‘Saudi’, ‘Russia’, ‘Japan’, ‘South Korea’, ‘Britain’, ‘Germany’, ‘France’]

percent = [0.5548467, 0.14444868, 0.05094268, ‘NaN’, 0.04846696, 0.046753, 0.04418206, ‘NaN’, 0.04161112, 0.03799276, 0.03075605]

dict = {mark[i]: percent[i] for i in range(len(mark))}

df = pd.Series(dict)

df = df.replace(“NaN”, np.nan)
df = df.dropna()

plt.figure(figsize=(10, 10), dpi=80) # 设置画布大小,清晰度

patches,l_text,p_text = plt.pie(
df.values, # 百分比
labels=df.index, # 名称
explode=(0, 0.1, 0, 0, 0, 0, 0, 0.1, 0), # 突出块,突出比例
autopct=’%1.1f%%’, # 显示百分比方式
shadow=False, # 阴影效果
startangle=-110, # 饼图起始的角度,度数,默认0为右侧水平180度开始,逆时针旋转
)

plt.title(“各国军费比例饼图”, color=‘g’, size=20) # 设置图标题,字体颜色,字体大小

添加图例,loc表示图例位置,bbox_to_anchor表示图例距画布的边距(外边距,上边,右边), ncol分为几列,borderaxespad图例内边距

plt.legend([‘美国’, ‘中国’, ‘印度’, ‘沙特’, ‘俄罗斯’, ‘日本’, ‘英国’, ‘德国’, ‘法国’], loc=‘upper right’, fontsize=10, bbox_to_anchor=(1, 1), ncol=2, borderaxespad=0.1)

plt.axis(“equal”) # 正圆形饼图,x/y轴尺寸相等,默认是扁图

设置字体大小

for t in l_text:
t.set_size(15)
for t in p_text:
t.set_size(15)

plt.savefig("./images/pie.png")
plt.show()

气泡图

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

data = {‘animal’: [‘cat’, ‘cat1’, ‘snake’, ‘dog’, ‘dog1’, ‘cat2’, ‘snake1’, ‘cat3’, ‘dog2’, ‘dog3’],

    'age': [2.5, 3, 0.5, np.nan, 5, 2, 4.5, np.nan, 7, 3],

    'visits': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],

    'priority': ['yes', 'yes', 'no', 'yes', 'no', 'no', 'no', 'yes', 'no', 'no']}

s1 = pd.DataFrame(data)
s2 = s1.dropna()

plt.rcParams[“font.sans-serif”] = [“SimHei”]

plt.figure(figsize=(10,10),dpi=60)

plt.scatter(
s2[“animal”],
s2[“age”]
)

plt.title(“动物编号对应年龄”)
plt.xlabel(“动物编号”,fontsize=20)
plt.ylabel(“动物年龄”,fontsize=20)

plt.savefig("./animal_age.png")

plt.show()

条形图

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

data = {‘animal’: [‘cat’, ‘cat1’, ‘snake’, ‘dog’, ‘dog1’, ‘cat2’, ‘snake1’, ‘cat3’, ‘dog2’, ‘dog3’],

    'age': [2.5, 3, 0.5, np.nan, 5, 2, 4.5, np.nan, 7, 3],

    'visits': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],

    'priority': ['yes', 'yes', 'no', 'yes', 'no', 'no', 'no', 'yes', 'no', 'no']}

s1 = pd.DataFrame(data)
s2 = s1.replace(np.nan,s1.age.mean()) #将年龄替换成平均值
s3 = s1.dropna()

plt.rcParams[“font.sans-serif”] = [“SimHei”]

plt.figure(figsize=(10,10),dpi=60)

plt.bar(
s3.animal,
s3.visits,
)

plt.title(“每只动物访问次数”)
plt.xlabel(“动物编号”)
plt.ylabel(“访问次数”)

plt.savefig("./animal_visit.png")

plt.show()

相关标签: 学习 plt