python Plot 画图用法
以下内容全部转载 粘贴复制于:CS青雀,重附连接:https://blog.csdn.net/ztf312/article/details/49933497
仅作自行查阅方便只用!!
图的存在,让数据变得形象化。无论多么复杂的东西,都是简单的组合。
plot画图时可以设定线条参数。包括:颜色、线型、标记风格。
1)控制颜色
颜色之间的对应关系为
b---blue c---cyan g---green k----black
m---magenta r---red w---white y----yellow
有三种表示颜色的方式:
a:用全名 b:16进制如:#FF00FF c:RGB或RGBA元组(1,0,1,1) d:灰度强度如:‘0.7’
2)控制线型
符号和线型之间的对应关系
- 实线
-- 短线
-. 短点相间线
: 虚点线
>>>import matplotlib>>>from pylab import *
>>>help(plot)
Help on function plot in module matplotlib.pyplot:plot(*args, **kwargs)Plot lines and/or markers to the:class:`~matplotlib.axes.Axes`. *args* is a variable lengthargument, allowing for multiple *x*, *y* pairs with anoptional format string. For example, each of the following islegal::plot(x, y) # plot x and y using default line style and colorplot(x, y, 'bo') # plot x and y using blue circle markersplot(y) # plot y using x as index array 0..N-1plot(y, 'r+') # ditto, but with red plussesIf *x* and/or *y* is 2-dimensional, then the corresponding columnswill be plotted.An arbitrary number of *x*, *y*, *fmt* groups can bespecified, as in::a.plot(x1, y1, 'g^', x2, y2, 'g-')Return value is a list of lines that were added.The following format string characters are accepted to controlthe line style or marker:================ ===============================character description================ ===============================``'-'`` solid line style``'--'`` dashed line style``'-.'`` dash-dot line style``':'`` dotted line style``'.'`` point marker``','`` pixel marker``'o'`` circle marker``'v'`` triangle_down marker``'^'`` triangle_up marker``'<'`` triangle_left marker``'>'`` triangle_right marker``'1'`` tri_down marker``'2'`` tri_up marker``'3'`` tri_left marker``'4'`` tri_right marker``'s'`` square marker``'p'`` pentagon marker``'*'`` star marker``'h'`` hexagon1 marker``'H'`` hexagon2 marker``'+'`` plus marker``'x'`` x marker``'D'`` diamond marker``'d'`` thin_diamond marker``'|'`` vline marker``'_'`` hline marker================ ===============================The following color abbreviations are supported:========== ========character color========== ========'b' blue'g' green'r' red'c' cyan'm' magenta'y' yellow'k' black'w' white========== ========In addition, you can specify colors in many weird andwonderful ways, including full names (``'green'``), hexstrings (``'#008000'``), RGB or RGBA tuples (``(0,1,0,1)``) orgrayscale intensities as a string (``'0.8'``). Of these, thestring specifications can be used in place of a ``fmt`` group,but the tuple forms can be used only as ``kwargs``.Line styles and colors are combined in a single format string, as in``'bo'`` for blue circles.The *kwargs* can be used to set line properties (any property that hasa ``set_*`` method). You can use this to set a line label (for autolegends), linewidth, anitialising, marker face color, etc. Here is anexample::plot([1,2,3], [1,2,3], 'go-', label='line 1', linewidth=2)plot([1,2,3], [1,4,9], 'rs', label='line 2')axis([0, 4, 0, 10])legend()If you make multiple lines with one plot command, the kwargsapply to all those lines, e.g.::plot(x1, y1, x2, y2, antialised=False)Neither line will be antialiased.You do not need to use format strings, which are justabbreviations. All of the line properties can be controlledby keyword arguments. For example, you can set the color,marker, linestyle, and markercolor with::plot(x, y, color='green', linestyle='dashed', marker='o',markerfacecolor='blue', markersize=12). See:class:`~matplotlib.lines.Line2D` for details.The kwargs are :class:`~matplotlib.lines.Line2D` properties:agg_filter: unknownalpha: float (0.0 transparent through 1.0 opaque) animated: [True | False] antialiased or aa: [True | False] axes: an :class:`~matplotlib.axes.Axes` instance clip_box: a :class:`matplotlib.transforms.Bbox` instance clip_on: [True | False] clip_path: [ (:class:`~matplotlib.path.Path`, :class:`~matplotlib.transforms.Transform`) | :class:`~matplotlib.patches.Patch` | None ] color or c: any matplotlib color contains: a callable function dash_capstyle: ['butt' | 'round' | 'projecting'] dash_joinstyle: ['miter' | 'round' | 'bevel'] dashes: sequence of on/off ink in points data: 2D array (rows are x, y) or two 1D arrays drawstyle: [ 'default' | 'steps' | 'steps-pre' | 'steps-mid' | 'steps-post' ] figure: a :class:`matplotlib.figure.Figure` instance fillstyle: ['full' | 'left' | 'right' | 'bottom' | 'top'] gid: an id string label: any string linestyle or ls: [ ``'-'`` | ``'--'`` | ``'-.'`` | ``':'`` | ``'None'`` | ``' '`` | ``''`` ] and any drawstyle in combination with a linestyle, e.g. ``'steps--'``. linewidth or lw: float value in points lod: [True | False] marker: [ ``7`` | ``4`` | ``5`` | ``6`` | ``'o'`` | ``'D'`` | ``'h'`` | ``'H'`` | ``'_'`` | ``''`` | ``None`` | ``'None'`` | ``' '`` | ``'8'`` | ``'p'`` | ``','`` | ``'+'`` | ``'.'`` | ``'s'`` | ``'*'`` | ``'d'`` | ``3`` | ``0`` | ``1`` | ``2`` | ``'1'`` | ``'3'`` | ``'4'`` | ``'2'`` | ``'v'`` | ``'<'`` | ``'>'`` | ``'^'`` | ``'|'`` | ``'x'`` | ``'$...$'`` | *tuple* | *Nx2 array* ]markeredgecolor or mec: any matplotlib color markeredgewidth or mew: float value in points markerfacecolor or mfc: any matplotlib color markerfacecoloralt or mfcalt: any matplotlib color markersize or ms: float markevery: None | integer | (startind, stride)picker: float distance in points or callable pick function ``fn(artist, event)`` pickradius: float distance in points rasterized: [True | False | None] snap: unknownsolid_capstyle: ['butt' | 'round' | 'projecting'] solid_joinstyle: ['miter' | 'round' | 'bevel'] transform: a :class:`matplotlib.transforms.Transform` instance url: a url string visible: [True | False] xdata: 1D array ydata: 1D array zorder: any number kwargs *scalex* and *scaley*, if defined, are passed on to:meth:`~matplotlib.axes.Axes.autoscale_view` to determinewhether the *x* and *y* axes are autoscaled; the default is*True*.Additional kwargs: hold = [True|False] overrides default hold state
自用小代码:
from matplotlib import pyplot as pltfilename = './loss.txt'
step, v_loss, gan = [], [], []
j = 0
i = 1
# 相比open(),with open()不用手动调用close()方法
with open(filename, 'r') as f:# 将txt中的数据逐行存到列表lines里 lines的每一个元素对应于txt中的一行。然后将每个元素中的不同信息提取出来lines = f.readlines()# i变量,由于这个txt存储时有空行,所以增只读偶数行,主要看txt文件的格式,一般不需要# j用于判断读了多少条,step为画图的X轴for line in lines:if i > 1600:step.append(30 * i)v_loss.append(float(line))gan.append(float(0.2))i = i + 1else:i = i + 1# temp = line.split('loss ')# t = temp[1].split(',')# step.append(30*j)# j = j + 1# v_loss.append(float(line))# i = i + 1fig = plt.figure(figsize=(10, 5)) # 创建绘图窗口,并设置窗口大小
# 画第一张图
ax1 = fig.add_subplot(111) # 将画面分割为2行1列选第一个
ax1.plot(step, v_loss, 'blue', label='Loss', linewidth=1) # 画dis-loss的值,颜色红
ax1.legend(loc='upper right') # 绘制图例,plot()中的label值
ax1.set_xlabel('step') # 设置X轴名称
ax1.set_ylabel('Training loss') # 设置Y轴名称
# ax1.set_title('Training Loss for U-Net')
# plt.show()# 画第二张图
# ax2 = fig.add_subplot(212) # 将画面分割为2行1列选第二个
# ax2.plot(step, gan, 'blue', label='gan') # 画gan-loss的值,颜色蓝
# ax2.legend(loc='upper right') # loc为图例位置,设置在右上方,(右下方为lower right)
# ax2.set_xlabel('step')
# ax2.set_ylabel('Generator-loss')
# plt.show() # 显示绘制的图# plt.figure()
# plt.plot(step, dis, 'red', label='dis')
plt.plot(step, gan, 'red', label='y=0.2', linewidth=3, linestyle="--")
plt.legend()
plt.show()
python Plot 画图用法相关推荐
- python plot画图_1. 用Python画出漂亮的图表
Python 非常简单而又非常强大,它的功能之一就是画出漂亮的图表,实现数据的可视化.在 Matplotlib 帮助下,它能画出多种图表.下面是 Matplotlib 的部分例子. 怎么样?很强大吧! ...
- Python plot() 画图标记 marker
这个链接中给出了最为详细的画图中 marker的定义方法,亲测有效,给大家推荐一波! https://www.cnblogs.com/qccc/p/12819459.html
- 为什么python画不了图-解决python中使用plot画图,图不显示的问题
python matplotlib画的图怎么显示不出来 初学python,matplotlib库画图不显示分享助别熬夜了,你等不来的人,等到多晚都不会来的. 见图.python matplotlib ...
- python怎么画参数函数图像_详解pandas.DataFrame.plot() 画图函数
首先看官网的DataFrame.plot( )函数 DataFrame.plot(x=None, y=None, kind='line', ax=None, subplots=False, share ...
- python matlabplot写字_Matlab中plot基本用法的具体使用
本文主要介绍了Matlab中plot基本用法的具体使用,分享给大家,具体如下: >> y=[0 0.58 0.70 0.95 0.83 0.25]; >> plot(y) 生成 ...
- python画图程序没有图_解决python中使用plot画图,图不显示的问题
解决python中使用plot画图,图不显示的问题 对以下数据画图结果图不显示,修改过程如下 df3 = {'chinese':109, 'American':88, 'German': 66, 'K ...
- 【Python】matplotlib.plot画图横坐标混乱及间隔处理
今天用matplotlib.plot画折线图图时发现横坐标并没有按顺序排列,出现了混乱的情况,导致图很乱,查了好多方法发现是数据类型的原因(最近我出现的好多问题都是因为数据类型不对,下次出问题要记得用 ...
- python 折线图 尾部_Matplotlib 折线图plot()所有用法详解
散点图和折线图是数据分析中最常用的两种图形.其中,折线图用于分析自变量和因变量之间的趋势关系,最适合用于显示随着时间而变化的连续数据,同时还可以看出数量的差异,增长情况. Matplotlib 中绘制 ...
- Python Matplotlib基本用法
Python Matplotlib基本用法 1. 认识Matploblib 1.1 Figure 1.2 Axes 1.3 Multiple Axes 1.4 Axes Vs .pyplot 2. 基 ...
- python plot参数,python plot函数参数
python中plot怎么设置横纵坐标名称 用plot画二维图像时,默认情况下的横坐标和纵坐标显示的值有时达不到自己的需CSS布局HTML小编今天和大家分享,需要借助xticks()和yticks() ...
最新文章
- EDIUS输出到文件
- fzu 1686(DLX 重复点覆盖)
- Elasticsearch索引分析
- vmware 克隆后Linux没有eth网卡只有lo
- aws lambda_跑来跑去:假人与AWS Lambda的第一次接触
- 线程、同步与锁——Mutex想说爱你不容易
- mysql/mariadb 安装开启spinx引擎
- FCM——(Fuzzy C-means)模糊C均值算法
- programmer-common-word-pronunciation 程序员常用单词发音
- 感冒初期试试食疗方 盲目用药伤脾胃
- 2005岁末BLOG程序大评点
- java 挡泥板,Canyon推出Grail AL铝合金Gravel Bike
- 游戏解说是咋制作的?怎么给视频添加有趣的剧情配音?
- 计算机程序设计基础----习题4
- java插入flash_如何在java中添加flash?
- 万一的 Delphi 博客 (注意 delphi的新功能)
- 数字孪生电梯模型构建方案
- Outlook 2007 电子邮件数字签名和加密指南
- css逐渐消失,css如何实现渐渐消失
- 企业自己如何快速开发一个简单实用的CRM客户管理系统?