视觉/图像重磅干货,第一时间送达!
使用 Pyplot 模块,将图表视为一个整体; 通过面向对象的界面,当每个形状或其一部分都是单独的对象时,这允许您有选择地更改它们的属性和显示。
pip3 install matplotlib
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [24, 29, 33, 19, 24]
plt.plot(x, y)
plt.show()
plt.plot(x, y)
plt.xlabel('x-axis') #name of the x-axis
plt.ylabel('y-axis') #name of the y-axis
plt.title('First graph') #name of the graph
plt.show()
plt.plot(x, y, color='purple', marker='o', markersize=8)
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [19, 24, 33, 22, 27, 21, 20, 30, 17, 26]
plt.scatter(x, y, color='purple')
plt.show()
x = ['January', 'February', 'March', 'April', 'May']
y = [2, 4, 3, 1, 7]
plt.bar(x, y, color='purple', label='Profit amount') #label parameter allows you to set the name of the value for the legend
plt.xlabel('Month')
plt.ylabel('$ Profit (in million)')
plt.title('Barchart Example')
plt.legend()
plt.show()
x = ['January', 'February', 'March', 'April', 'May']
y = [2, 4, 3, 1, 7]
plt.bar(x, y, color='purple', label='Profit amount') #label parameter allows you to set the name of the value for the legend
plt.plot(x, y, color='blue', marker='o', markersize=8)
plt.xlabel('Month')
plt.ylabel('$ Profit (in million)')
plt.title('Combining Graphs')
plt.legend()
plt.show()
一切都顺利。但现在折线图很难看清——它只是在柱状图的紫色背景中消失了。让我们使用参数增加条形图的透明度alpha:
plt.bar(x, y, label='Profit amount', alpha=0.5)
values = [23, 16, 55, 19, 37]
labels = ["Honda", "Mazda", "BMW", "Mercedes", "Acura"]
colors = ( "#9E70F1", "#F52B94", "#FFE174", "#65E9EB", '#F3907F') #identify colors
plt.pie(values, labels=labels, colors=colors)
plt.title("Distribution of Car Brands on the Road")
plt.show()
values = [23, 16, 55, 19, 37]
labels = ["Honda", "Mazda", "BMW", "Mercedes", "Acura"]
colors = ( "#9E70F1", "#F52B94", "#FFE174", "#65E9EB", '#F3907F') #identify colors
plt.pie(values, labels=labels, colors=colors, autopct='%1.1f%%')
plt.title("Distribution of Car Brands on the Road")
plt.show()
labels = ['2019', '2020', '2021', '2022', '2023']
users_ios = [52, 52.5, 53.2, 53, 57]
users_android = [48, 47.5, 46.8, 47, 43]
width = 0.35 #set column width
fig, ax = plt.subplots()
ax.bar(labels, users_ios, width, color='#F52B94', label='iOS')
ax.bar(labels, users_android, width,
bottom=users_ios, color='#15C0C3', label='Android') #indicate with the bottom parameter that the values in the column should be higher than the values of the ios_users variable
ax.set_ylabel('Ratio %')
ax.set_title('Distribution of iOS and Android Devices in US')
ax.legend(loc='lower left', title='Devices') #shift the legend to the lower left corner so that it does not overlap part of the chart
plt.show()
https://matplotlib.org/3.6.2/users/index.html
—THE END—
觉得有用,麻烦给个赞和在看
觉得有用,麻烦给个赞和在看