matplotlib国内热门旅游城市饼图
时间: 2023-07-12 15:34:26 浏览: 44
好的,我可以为您生成一个matplotlib的热门旅游城市饼图。以下是代码示例:
```python
import matplotlib.pyplot as plt
# 数据
cities = ['北京', '上海', '成都', '广州', '杭州']
populations = [1247, 2440, 1635, 1404, 981]
# 颜色
colors = ['#ff9999','#66b3ff','#99ff99','#ffcc99','#ffccff']
# 饼图
plt.pie(populations, labels=cities, colors=colors, autopct='%1.1f%%', startangle=90)
# 图片属性设置
plt.axis('equal')
plt.title('中国热门旅游城市')
# 显示图像
plt.show()
```
这段代码将生成一个饼图,展示了中国热门旅游城市的相对受欢迎程度。您可以根据需要更改城市名称和人口数据。
相关问题
matplotlib国内旅游热门城市中文饼状图
好的,以下是实现该需求的代码:
```python
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager
# 设置中文字体
font_path = 'path/to/your/font.ttf' # 替换成你的中文字体路径
font_prop = font_manager.FontProperties(fname=font_path)
# 数据
cities = ['北京', '上海', '成都', '重庆', '广州']
counts = [120, 90, 80, 70, 60]
# 绘制饼状图
plt.figure(figsize=(6,6)) # 图片大小为 6x6 寸
plt.pie(counts, labels=cities, autopct='%1.1f%%', textprops={'fontproperties': font_prop})
# 设置 autopct='%1.1f%%' 表示保留一位小数并添加百分号,textprops 设置中文字体
plt.title('国内旅游热门城市', fontproperties=font_prop) # 设置标题
plt.show()
```
解释一下代码,首先需要设置中文字体,因为默认的字体不支持中文。然后定义了一个城市和对应数量的数据,接着使用 `plt.pie` 绘制饼状图,其中 `labels` 参数表示标签,`autopct` 表示自动计算百分比并显示,`textprops` 表示设置中文字体。最后设置标题并显示图像。
matplotlib国内旅游人数柱状图折线图饼图
好的,下面是一个简单的 matplotlib 可视化实例,展示如何使用 matplotlib 绘制国内旅游人数的柱状图、折线图和饼图:
```python
import matplotlib.pyplot as plt
import numpy as np
# 生成数据
year = np.array([2015, 2016, 2017, 2018, 2019, 2020])
people = np.array([413, 452, 512, 577, 610, 255])
# 绘制柱状图
plt.bar(year, people)
# 添加标题和标签
plt.title('Domestic Tourism in China (Bar Chart)')
plt.xlabel('Year')
plt.ylabel('People (million)')
# 显示图形
plt.show()
# 绘制折线图
plt.plot(year, people)
# 添加标题和标签
plt.title('Domestic Tourism in China (Line Chart)')
plt.xlabel('Year')
plt.ylabel('People (million)')
# 显示图形
plt.show()
# 绘制饼图
labels = ['2015', '2016', '2017', '2018', '2019', '2020']
plt.pie(people, labels=labels, autopct='%1.1f%%')
# 添加标题
plt.title('Domestic Tourism in China (Pie Chart)')
# 显示图形
plt.show()
```
在这个例子中,我们先使用`numpy`库生成了一组国内旅游人数的数据,然后分别使用`bar`函数绘制了柱状图,`plot`函数绘制了折线图,和`pie`函数绘制了饼图。最后添加了标题和标签,并使用`show`函数显示图形。
如果你想绘制其他类型的可视化图表,可以根据具体需求选择不同的函数和参数。具体可以查看matplotlib官方文档。