python
2024-11-06 13:40:50 0 举报
AI智能生成
图
作者其他创作
大纲/内容
作图
图1:折线图
import matplotlib.pyplot as plt
plt.rcParams["font.sans-serif"]=["SimHei"] #设置字体
plt.rcParams["axes.unicode_minus"]=False #该语句解决图像中的“-”负号的乱码问题
xpoints = np.array([0, 10])
ypoints = np.array([0, 60])
plt.xlabel("天数")
plt.ylabel("收入")
plt.plot(x)
plt.plot(y)
plt.show()
plt.rcParams["font.sans-serif"]=["SimHei"] #设置字体
plt.rcParams["axes.unicode_minus"]=False #该语句解决图像中的“-”负号的乱码问题
xpoints = np.array([0, 10])
ypoints = np.array([0, 60])
plt.xlabel("天数")
plt.ylabel("收入")
plt.plot(x)
plt.plot(y)
plt.show()
图2:柱状图
x=[1,2,3,4,5,6,7,8,9,10]
data=[5,10,23,45,23,42,43,32,43,54]
plt.title("")
plt.grid(ls="--", alpha=1)
plt.bar(x, data ,facecolor="r")
data=[5,10,23,45,23,42,43,32,43,54]
plt.title("")
plt.grid(ls="--", alpha=1)
plt.bar(x, data ,facecolor="r")
子主题
子主题
对比柱状图
import matplotlib.pyplot as plt
import numpy as np
labels = ['G1', 'G2', 'G3', 'G4', 'G5']
men_means = [20, 34, 30, 35, 27]
women_means = [25, 32, 34, 20, 25]
x = np.arange(len(labels)) # the label locations
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, men_means, width, label='Men')
rects2 = ax.bar(x + width/2, women_means, width, label='Women')
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(x, labels)
ax.legend()
ax.bar_label(rects1, padding=3)
ax.bar_label(rects2, padding=3)
fig.tight_layout()
plt.show()
import numpy as np
labels = ['G1', 'G2', 'G3', 'G4', 'G5']
men_means = [20, 34, 30, 35, 27]
women_means = [25, 32, 34, 20, 25]
x = np.arange(len(labels)) # the label locations
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, men_means, width, label='Men')
rects2 = ax.bar(x + width/2, women_means, width, label='Women')
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(x, labels)
ax.legend()
ax.bar_label(rects1, padding=3)
ax.bar_label(rects2, padding=3)
fig.tight_layout()
plt.show()
子主题
图3:饼图
子主题
import matplotlib.pyplot as plt
import numpy as np
y = np.array([35, 25, 25, 15])
plt.pie(y)
labels=['A','B','C','D'], # 设置饼图标
colors=["red", "blue", "yellow", "purple"], # 设置饼图颜色
plt.title("各组占比") # 设置标题
plt.show()
import numpy as np
y = np.array([35, 25, 25, 15])
plt.pie(y)
labels=['A','B','C','D'], # 设置饼图标
colors=["red", "blue", "yellow", "purple"], # 设置饼图颜色
plt.title("各组占比") # 设置标题
plt.show()
子主题
0 条评论
下一页