pyplot 산점도

1 개요

파이썬 산점도
pyplot 산점도

2 예시 1: iris

Python
CPU
1.2s
MEM
77M
2.5s
Copy
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/jmnote/zdata/master/R/iris.csv')
print( df )

import matplotlib.pyplot as plt
plt.scatter(df['sepal_length'], df['sepal_width'])
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')
plt.show()
     Unnamed: 0  Sepal.Length  ...  Petal.Width    Species
0             1           5.1  ...          0.2     setosa
1             2           4.9  ...          0.2     setosa
2             3           4.7  ...          0.2     setosa
3             4           4.6  ...          0.2     setosa
4             5           5.0  ...          0.2     setosa
..          ...           ...  ...          ...        ...
145         146           6.7  ...          2.3  virginica
146         147           6.3  ...          1.9  virginica
147         148           6.5  ...          2.0  virginica
148         149           6.2  ...          2.3  virginica
149         150           5.9  ...          1.8  virginica

[150 rows x 6 columns]
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 3361, in get_loc
    return self._engine.get_loc(casted_key)
  File "pandas/_libs/index.pyx", line 76, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 5198, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 5206, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'sepal_length'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "runbox.py", line 6, in <module>
    plt.scatter(df['sepal_length'], df['sepal_width'])
  File "/usr/local/lib/python3.8/site-packages/pandas/core/frame.py", line 3458, in __getitem__
    indexer = self.columns.get_loc(key)
  File "/usr/local/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 3363, in get_loc
    raise KeyError(key) from err
KeyError: 'sepal_length'
Command exited with non-zero status 1

3 예시 2: iris 색상

Python
Copy
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/jmnote/zdata/master/R/iris.csv')

x = df['Sepal.Length']
y = df['Sepal.Width']
colors = df['Species'].astype("category").cat.codes

import matplotlib.pyplot as plt
plt.scatter(x, y, c=colors)
plt.xlabel('sepal length')
plt.ylabel('sepal width')
plt.show()
Loading

4 같이 보기