Skip to content

Commit a36b777

Browse files
committed
scatter
1 parent 640024a commit a36b777

5 files changed

Lines changed: 110 additions & 0 deletions

File tree

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,44 @@ heatmap_plot = sns.heatmap(data, center=0, cmap='gist_rainbow')
670670
plt.show()
671671
```
672672
![热力图](./img/heatmap.png)
673+
2 散点图
674+
```python
675+
import numpy as np
676+
import pandas as pd
677+
import matplotlib as mpl
678+
import matplotlib.pyplot as plt
679+
680+
# 版本检查
681+
print(mpl.__version__) #> 3.0.0
682+
683+
684+
# 导入数据集
685+
midwest = pd.read_csv("https://raw.githubusercontent.com/selva86/datasets/master/midwest_filter.csv")
686+
687+
688+
# midwest['category']分类,颜色设置为与其一样多
689+
categories = np.unique(midwest['category'])
690+
colors = [plt.cm.tab10(i/float(len(categories)-1)) for i in range(len(categories))]
691+
692+
# 每个分类plot
693+
plt.figure(figsize=(16, 10), dpi= 80, facecolor='w', edgecolor='k')
694+
695+
for i, category in enumerate(categories):
696+
plt.scatter('area', 'poptotal',
697+
data=midwest.loc[midwest.category==category, :],
698+
s=20, c=colors[i], label=str(category))
699+
700+
# 修改x轴,y轴坐标系尺寸区间
701+
plt.gca().set(xlim=(0.0, 0.1), ylim=(0, 90000),
702+
xlabel='Area', ylabel='Population')
703+
704+
plt.xticks(fontsize=12)
705+
plt.yticks(fontsize=12)
706+
plt.title("Midwest Area vs Population", fontsize=22)
707+
plt.legend(fontsize=12)
708+
plt.show()
709+
```
710+
![散点图](./img/scatter-plt.png)
673711

674712
#### 十三、PyQt
675713

img/scatter-plt.png

50 KB
Loading

md/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
2. [turtle绘制漫天雪花](turtle绘制漫天雪花.md)
7777
3. [4种不同颜色的色块,它们的颜色真的不同吗?](4种不同颜色的色块,它们的颜色真的不同吗?.md)
7878
4. [词频云图](词频云图.md)
79+
5. [散点图](散点图.md)
7980

8081

8182
#### 九、生成器

md/散点图.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
```python
2+
import numpy as np
3+
import pandas as pd
4+
import matplotlib as mpl
5+
import matplotlib.pyplot as plt
6+
7+
# 版本检查
8+
print(mpl.__version__) #> 3.0.0
9+
10+
11+
# 导入数据集
12+
midwest = pd.read_csv("https://raw.githubusercontent.com/selva86/datasets/master/midwest_filter.csv")
13+
14+
15+
# midwest['category']分类,颜色设置为与其一样多
16+
categories = np.unique(midwest['category'])
17+
colors = [plt.cm.tab10(i/float(len(categories)-1)) for i in range(len(categories))]
18+
19+
# 每个分类plot
20+
plt.figure(figsize=(16, 10), dpi= 80, facecolor='w', edgecolor='k')
21+
22+
for i, category in enumerate(categories):
23+
plt.scatter('area', 'poptotal',
24+
data=midwest.loc[midwest.category==category, :],
25+
s=20, c=colors[i], label=str(category))
26+
27+
# 修改x轴,y轴坐标系尺寸区间
28+
plt.gca().set(xlim=(0.0, 0.1), ylim=(0, 90000),
29+
xlabel='Area', ylabel='Population')
30+
31+
plt.xticks(fontsize=12)
32+
plt.yticks(fontsize=12)
33+
plt.title("Midwest Area vs Population", fontsize=22)
34+
plt.legend(fontsize=12)
35+
plt.show()
36+
37+
```

src/scatter.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import numpy as np
2+
import pandas as pd
3+
import matplotlib as mpl
4+
import matplotlib.pyplot as plt
5+
6+
# 版本检查
7+
print(mpl.__version__) #> 3.0.0
8+
9+
10+
# 导入数据集
11+
midwest = pd.read_csv("https://raw.githubusercontent.com/selva86/datasets/master/midwest_filter.csv")
12+
13+
14+
# midwest['category']分类,颜色设置为与其一样多
15+
categories = np.unique(midwest['category'])
16+
colors = [plt.cm.tab10(i/float(len(categories)-1)) for i in range(len(categories))]
17+
18+
# 每个分类plot
19+
plt.figure(figsize=(16, 10), dpi= 80, facecolor='w', edgecolor='k')
20+
21+
for i, category in enumerate(categories):
22+
plt.scatter('area', 'poptotal',
23+
data=midwest.loc[midwest.category==category, :],
24+
s=20, c=colors[i], label=str(category))
25+
26+
# 修改x轴,y轴坐标系尺寸区间
27+
plt.gca().set(xlim=(0.0, 0.1), ylim=(0, 90000),
28+
xlabel='Area', ylabel='Population')
29+
30+
plt.xticks(fontsize=12)
31+
plt.yticks(fontsize=12)
32+
plt.title("Midwest Area vs Population", fontsize=22)
33+
plt.legend(fontsize=12)
34+
plt.show()

0 commit comments

Comments
 (0)