Python:matplotlib绘图
1.Python:matplotlib繪圖時指定圖像大小,放大圖像
matplotlib繪圖時是默認的大小,有時候默認的大小會感覺圖片里的內容都被壓縮了,解決方法如下。
先是原始代碼:
| 1 2 3 4 5 6 7 | from?matplotlib?import?pyplot as plt ? plt.figure(figsize=(1,1)) #單位是英寸 ? x?=?[1,2,3] plt.plot(x, x) plt.show() |
關鍵的代碼是plt.figure(figsize=(1,1)),生成的圖片如下
修改代碼,放大圖片:
| 1 2 3 4 5 6 7 | from?matplotlib?import?pyplot as plt ? plt.figure(figsize=(10,10)) ? x?=?[1,2,3] plt.plot(x, x) plt.show() |
這時候橫坐標和縱坐標都放大了10倍:
如果想要指定像素,可以這么做:
| 1 2 3 4 5 6 7 | from?matplotlib?import?pyplot as plt ? plt.figure(dpi=80) ? x?=?[1,2,3] plt.plot(x, x) plt.show() |
?
2.Python:matplotlib繪圖-scatter 散點圖
https://matplotlib.org/gallery/lines_bars_and_markers/scatter_custom_symbol.html#sphx-glr-gallery-lines-bars-and-markers-scatter-custom-symbol-py
import matplotlib.pyplot as plt import numpy as np# unit area ellipse rx, ry = 3., 1. area = rx * ry * np.pi theta = np.arange(0, 2 * np.pi + 0.01, 0.1) verts = np.column_stack([rx / area * np.cos(theta), ry / area * np.sin(theta)])x, y, s, c = np.random.rand(4, 30) s *= 10**2.fig, ax = plt.subplots() ax.scatter(x, y, s, c, marker=verts)plt.show()?
總結
以上是生活随笔為你收集整理的Python:matplotlib绘图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 常用Python代码
- 下一篇: Python之值得学习练手的22个迷你程