首页 > 其他分享 > Matplotlib绘制散点图与条形图

Matplotlib绘制散点图与条形图

时间:2023-01-16 21:46:54浏览次数:30  
标签:10 plt 散点图 Matplotlib fontproperties font my 条形图

Matplotlib绘制散点图与条形图

绘制散点图

# 绘制散点图

from matplotlib import pyplot as plt
from matplotlib import font_manager

# C:\Windwos\Fonts\msyh.ttc
my_font = font_manager.FontProperties(fname="C:\Windows\Fonts\simhei.ttf")

y_3 = [11, 17, 16, 11, 12, 11, 12, 6, 6, 7, 8, 9, 12, 15, 14, 17,
       18, 21, 16, 17, 20, 14, 15, 15, 15, 19, 21, 22, 22, 22, 23]
y_10 = [26, 26, 28, 19, 21, 17, 16, 19, 18, 20, 20, 19, 22, 23, 17,
        20, 21, 20, 22, 15, 11, 15, 5, 13, 17, 10, 11, 13, 12, 13, 6]

x_3 = range(0, 31)
x_10 = range(40, 71)

# 这是图形大小
plt.figure(figsize=(20, 8), dpi=80)

# 使用scatter绘制散点图
plt.scatter(x_3, y_3, label="3月份")
plt.scatter(x_10, y_10, label="10月份")

# 调整X的刻度
_x = list(x_3) + list(x_10)
_xtick_lables = ["3月{0}日".format(i) for i in x_3]
_xtick_lables += ["10月{0}日".format(i) for i in x_10]
plt.xticks(_x[::3], _xtick_lables[::3], fontproperties=my_font, rotation=45)

# 添加图例
plt.legend(loc="upper left", prop=my_font)

# 添加标题
plt.xlabel("月份", fontproperties=my_font)
plt.ylabel("温度", fontproperties=my_font)
plt.title("3月份与10月份温度对比散点图", fontproperties=my_font)

# 展示
plt.show()


绘制条形图

# 绘制条形图

from matplotlib import pyplot as plt
from matplotlib import font_manager

# 设置字体
my_font = font_manager.FontProperties(fname="C:\Windows\Fonts\simhei.ttf")

a = [
    "战狼2", "速度与激情8", "功夫瑜伽", "西游伏妖篇", "变形金刚5:最后的骑士", "摔跤吧!爸爸", "加勒比海盗5:死无对证", "金刚:骷髅岛", "极限特工:终极回归", "生化危机6:终章", "乘风破浪", "神偷奶爸3", "智取威虎山", "大闹天竺", "金刚狼3:殊死一战", "蜘蛛侠:英雄归来", "悟空传", "银河护卫队2", "情圣", "新木乃伊"
]

b = [
    56.01, 26.94, 17.53, 16.49, 15.45, 12.96, 11.8, 11.61, 11.28,
    11.12, 10.49, 10.3, 8.75, 7.55, 7.32, 6.99, 6.88, 6.86, 6.58, 6.23
]

# 调整图形大小
plt.figure(figsize=(16, 6), dpi=90)

# 绘制图形
# plt.bar是绘制树状条形图
plt.barh(range(len(a)), b, height=0.4, color="red")

# 设置Y轴刻度
plt.yticks(range(len(a)), a, fontproperties=my_font)

# 设置网格,para:alpha 透明度
plt.grid(alpha=0.4)


# 设置标签
plt.xlabel("票房 单位:亿元", fontproperties=my_font)
plt.ylabel("电影", fontproperties=my_font)
plt.title("电影票房统计", fontproperties=my_font)

# 展示
plt.show()


标签:10,plt,散点图,Matplotlib,fontproperties,font,my,条形图
From: https://www.cnblogs.com/thankcat/p/17056352.html

相关文章

  • Matplotlib 绘制折线图
    Matplotlibmatplotlib:最流行的Python底层绘图库,主要做数据可视化图表,名字取材于MATLAB,模仿MATLAB构建绘制折线图绘制两小时的温度变化frommatplotlibimportpyplo......
  • Matplotlib学习笔记1 - 上手制作一些图表吧!
    Matplotlib学习笔记1-上手制作一些图表吧!Matplotlib是一个面向Python的,专注于数据可视化的模块。快速上手这是使用频率最高的几个模块,在接下来的程序中,都需要把它们作......
  • Matplotlib 学习
    Matplotlib图像组成:Figure:指整个图形,您可以把它理解成一张画布,它包括了所有的元素,比如标题、轴线等;Axes:绘制2D图像的实际区域,也称为轴域区,或者绘图区;Axis:指坐标系中......
  • python利用matplotlib生成迷宫
    起因我想要写一个项目叫python迷宫游戏,需求是玩家能和机器对抗率先走出迷宫,至少要有两个等级的电脑。慢慢来,首先迷宫游戏需要有一个迷宫并展示出来,这便是这篇博客的目的......
  • 安装不了matplotlib库解决方法
    https://wizardforcel.gitbooks.io/matplotlib-user-guide/2.html安装不了matplotlib库,通过下面两条命令解决python-mpipinstall-Upipsetuptoolspython-mpipin......
  • Python+matplotlib实现折线图的美化
    1.导入包importpandasaspdimportmatplotlib.pyplotaspltimportmatplotlib.tickerastickerimportmatplotlib.gridspecasgridspec2.获得数据file_i......
  • 数据可视化大屏Echarts高级开发散点图实战案例分析(地图扩展插件bmap.min.js、散点图、
    系列文章目录​​燃,拿来即用!Echarts动态排名柱状图(自适应电脑和手机端)​​​​漏刻有时数据可视化Echarts组件开发(27):盒须图(箱线图)前后端php交互的实战案例​​​​漏......
  • 50matplotlib
    importmatplotlib.pyplotasplt#plt.plot([1,2,3,4,5],[3,1,5,6,9],'ro')#plt.show()#x=range(1,15)#y=range(1,42,3)#plt.plot(x,y,color='y')#......
  • Python矩阵作图库matplotlib的初级使用(2)
    基础介绍matplotlib图形对象层级结构:图形对象(figure)→子图对象(axes)→坐标轴对象(axis)→定位器对象-刻度线(locator)/格式化器对象-刻度线标签(formatter)......
  • matplotlib折线图
    目录​​1.假设一天中每隔两个小时(range(2,26,2))的气温(C)分别是[15,13,14.5,17,20,25,26,26,27,22,18,15],要求绘制出如下折线图​​​​2.要求绘制折线图观10点到12点......