python中用plt画图
时间: 2023-11-03 11:06:26 浏览: 119
在Python中,可以使用matplotlib库中的pyplot模块来进行绘图。通过使用plt对象,可以实现各种类型的图形绘制。以下是几个常见的使用plt绘图的例子:
1. 绘制多个子图:
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(2,1,1) # 画2行1列个图形的第1个
ax2 = fig.add_subplot(2,1,2) # 画2行1列个图形的第2个
ax1.plot(np.random.randint(1,5,5), np.arange(5))
ax2.plot(np.arange(10)*3, np.arange(10))
plt.show()
2. 绘制折线图:
import matplotlib.pyplot as plt
plt.plot(first_twelve['DATE'], first_twelve['VALUE'])
plt.show()
3. 设置边框样式:
import pandas as pd
import matplotlib.pyplot as plt
women_degrees = pd.read_csv('percent-bachelors-degrees-women-usa.csv')
fig, ax = plt.subplots()
ax.plot(women_degrees['Year'], women_degrees['Biology'], c='blue', label='Women')
ax.plot(women_degrees['Year'], 100-women_degrees['Biology'], c='green', label='Men')
ax.tick_params(bottom="on", top="on", left="off", right="off")
for key,spine in ax.spines.items():
spine.set_visible(False)
ax.legend(loc='upper right')
plt.show()
这些例子展示了如何使用plt对象来绘制图形,可以根据需要进行相应的修改和调整。
阅读全文
相关推荐


















