matplotlib - matplotlib 图形输出一团糟
问题描述
一直在尝试制作情节,但输出一团糟,代码似乎没有问题。谁能帮忙解释一下?
x = np.random.rand(10)
y = np.sin(x)
fig, ax = plt.subplots(111)
ax[0].plot(x, y)
plt.show()
fig.savefig("1.png")
解决方案
你正在路过plt.subplots(111)
。您正在传递 111 行:matplotlib.pyplot.subplots(nrows=1, ncols=1, *, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)
如果您只想要一个图表,您可以使用:
import matplotlib.pyplot as plt
import random
import numpy as np
x = np.random.rand(10)
y = np.sin(x)
fig, ax = plt.subplots()
ax.plot(x, y)
plt.show()
输出:
你的意思是使用matplotlib.pyplot.subplot(*args, **kwargs)
?
import matplotlib.pyplot as plt
import random
import numpy as np
x = np.random.rand(10)
y = np.sin(x)
plt.subplot(111)
ax = plt.subplot(1,1,1)
ax.plot(x,y)
plt.show()
输出:
另一个使用示例plt.subplot(311)
:
x = np.random.rand(10)
y = np.sin(x)
plt.subplot(311)
ax1 = plt.subplot(1,1,1)
ax1.plot(x,y,c='red')
plt.show()
ax2 = plt.subplot(2,1,1)
ax2.plot(x,y,c='green')
plt.show()
ax2 = plt.subplot(3,1,1)
ax2.plot(x,y,c='blue')
plt.show()
输出:
推荐阅读
- python - 将对象从另一个应用程序传输到 ChoiceField
- c# - 拆分字符串,给其中的每个变量一个值
- excel - 当另一个函数工作时关闭 Worksheet_Change
- ios - Swift 4:按 RSSI 值对 UITableViewCells 进行排序
- sql - JOIN 的 SQL 组合
- react-native - 欧元符号未显示在文本组件 React native 中
- c - Misra-C 与 X 宏兼容吗?
- android - 获取的联系号码显示重复号码(Whatsapp 和双人号码)
- google-play - 我可以要求谷歌搜索重新索引我的应用程序的播放商店列表吗?
- c# - 检查数据库中的空值