matplotlib画图(4)

import numpy as np
import matplotlib.pyplot as plt
t = np.arange(0.0, 2.0, 0.01)
s = np.sin(2*np.pi*t)
plt.plot(t,s)
plt.title(r'$\alpha_i > \beta_i$', fontsize=20)
plt.text(1, -0.6, r'$\sum_{i=0}^\infty x_i$', fontsize=20)
plt.text(0.6, 0.6, r'$\mathcal{A}\mathrm{sin}(2 \omega t)$',
fontsize=20)
plt.xlabel('time (s)')
plt.ylabel('volts (mV)')


 

 

import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, polar=True)
r = np.arange(0,1,0.001)
theta = 2*2*np.pi*r
line, = ax.plot(theta, r, color='#ee8d18', lw=3)
ind = 800
thisr, thistheta = r[ind], theta[ind]
ax.plot([thistheta], [thisr], 'o')
ax.annotate('a polar annotation',
	xy=(thistheta, thisr), # theta, radius
	xytext=(0.05, 0.05), # fraction, fraction
	textcoords='figure fraction',
	arrowprops=dict(facecolor='black', shrink=0.05),
	horizontalalignment='left',
	verticalalignment='bottom',
)
plt.show()


 

 

### 使用 Matplotlib 创建图表 Matplotlib 是一个功能强大的 Python 库,能够创建多种类型的图表和可视化图形。无论是数据科学家、工程师还是研究人员,都可以利用 Matplotlib 来探索数据并传达结果[^1]。 #### 安装 Matplotlib 要开始使用 Matplotlib,首先需要确保已安装该库。可以通过 pip 工具轻松完成安装: ```bash pip install matplotlib ``` #### 绘制简单的折线图 下面是一个绘制简单折线图的例子,展示了未来 15 天的最高气温和最低气温的变化趋势[^4]。 ```python import matplotlib.pyplot as plt # 数据准备 days = list(range(1, 16)) high_temps = [22, 24, 27, 30, 32, 34, 35, 36, 38, 39, 40, 41, 42, 43, 44] low_temps = [15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43] plt.figure(figsize=(10, 6)) # 折线图绘制 plt.plot(days, high_temps, label='High Temperatures', marker='o') plt.plot(days, low_temps, label='Low Temperatures', linestyle='--') # 添加标签和标题 plt.title('Temperature Trends Over the Next 15 Days', fontsize=16) plt.xlabel('Day', fontsize=12) plt.ylabel('Temperature (°C)', fontsize=12) # 显示图例 plt.legend() # 展示图表 plt.show() ``` 这段代码会生成一张显示未来 15 天内每天最高温度和最低温度变化趋势的折线图。通过 `marker` 参数可以设置数据点标记样式;而 `linestyle` 则用来指定线条风格。 #### 绘制饼图 对于展示各部分占整体比例的情况,可以选择使用饼图。这里给出一段关于四个分类项 A 至 D 的占比情况的示例代码[^5]。 ```python import matplotlib.pyplot as plt # 准备数据 labels = ['A', 'B', 'C', 'D'] sizes = [15, 30, 45, 10] colors = ['red', 'green', 'blue', 'yellow'] fig, ax = plt.subplots() # 饼图绘制 ax.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=90) # 设置图表标题 ax.set_title('Pie Chart of Data', fontsize=16) # 展示图表 plt.show() ``` 此段代码将生成一个四类别的饼形分布图,其中每个扇区代表一类项目所占的比例,并附带百分比标注以便于阅读。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值