Pandas: κΈ°μ‘΄ 좕을 μ‚¬μš©ν•˜μ—¬ DataFrameμ—μ„œ λ§‰λŒ€ 그림을 ν”Œλ‘œνŒ…ν•˜λŠ” λ™μ•ˆ 였λ₯˜ λ°œμƒ

에 λ§Œλ“  2016λ…„ 08μ›” 24일  Β·  3μ½”λ©˜νŠΈ  Β·  좜처: pandas-dev/pandas

μ½”λ“œ μƒ˜ν”Œ, κ°€λŠ₯ν•œ 경우 λ³΅μ‚¬ν•˜μ—¬ λΆ™μ—¬ 넣을 μˆ˜μžˆλŠ” 예제

λ‹€μŒ μ½”λ“œκ°€ μž‘λ™ν•˜λ©° Jupyter Notebook λ˜λŠ” QT μ½˜μ†”μ— λΆ™μ—¬ λ„£μœΌλ©΄ μ˜ˆμƒλ˜λŠ” λ§‰λŒ€ κ·Έλž˜ν”„κ°€ μƒμ„±λ©λ‹ˆλ‹€.

%matplotlib inline
import pandas as pd
import matplotlib.pyplot as plt

# Create some data to plot
df = pd.DataFrame(
        dict(Feature1=[10, 20, 30, 20, 10], Feature2=[3, 2, 3, 2, 3]),
        index=['a', 'b', 'c', 'd', 'e']
)
_ = df.plot(kind='bar', legend=False, y='Feature1', yerr='Feature2', rot=0)

κ·ΈλŸ¬λ‚˜ μ‚¬μš©μž 지정 Axes μΈμŠ€ν„΄μŠ€λ₯Ό μ œκ³΅ν•˜λ €κ³ ν•˜λ©΄ (κ·Έλž˜μ•Ό ν”Œλ‘―μ΄ μ›ν•˜λŠ” μœ„μΉ˜μ— 배치됨) 였λ₯˜κ°€ λ°œμƒν•©λ‹ˆλ‹€. μœ„μ˜ 예제λ₯Ό μ‹€ν–‰ ν•œ ν›„ λ‹€μŒ μ½”λ“œλ₯Ό μ‹€ν–‰ν•˜λ©΄ 였λ₯˜κ°€ λ°œμƒν•©λ‹ˆλ‹€.

fig = plt.figure(0)
ax = fig.add_subplot(111)
_ = df.plot(kind='bar', legend=False, y='Feature1', yerr='Feature2', rot=0, axes=ax)

λ‹€μŒμ€ μ—­ μΆ”μ μž…λ‹ˆλ‹€.

Traceback (most recent call last):
  File "<stdin>", line 12, in <module>
  File "/usr/local/miniconda3/lib/python3.5/site-packages/pandas/tools/plotting.py", line 3740, in __call__
    sort_columns=sort_columns, **kwds)
  File "/usr/local/miniconda3/lib/python3.5/site-packages/pandas/tools/plotting.py", line 2614, in plot_frame
    **kwds)
  File "/usr/local/miniconda3/lib/python3.5/site-packages/pandas/tools/plotting.py", line 2441, in _plot
    plot_obj.generate()
  File "/usr/local/miniconda3/lib/python3.5/site-packages/pandas/tools/plotting.py", line 1028, in generate
    self._make_plot()
  File "/usr/local/miniconda3/lib/python3.5/site-packages/pandas/tools/plotting.py", line 1971, in _make_plot
    log=self.log, **kwds)
  File "/usr/local/miniconda3/lib/python3.5/site-packages/pandas/tools/plotting.py", line 1918, in _plot
    return ax.bar(x, y, w, bottom=start, log=log, **kwds)
  File "/usr/local/miniconda3/lib/python3.5/site-packages/matplotlib/__init__.py", line 1812, in inner
    return func(ax, *args, **kwargs)
  File "/usr/local/miniconda3/lib/python3.5/site-packages/matplotlib/axes/_axes.py", line 2134, in bar
    self.add_patch(r)
  File "/usr/local/miniconda3/lib/python3.5/site-packages/matplotlib/axes/_base.py", line 1780, in add_patch
    self._set_artist_props(p)
  File "/usr/local/miniconda3/lib/python3.5/site-packages/matplotlib/axes/_base.py", line 865, in _set_artist_props
    a.axes = self
  File "/usr/local/miniconda3/lib/python3.5/site-packages/matplotlib/artist.py", line 237, in axes
    raise ValueError("Can not reset the axes.  You are "
ValueError: Can not reset the axes.  You are probably trying to re-use an artist in more than one Axes which is not supported

μ˜ˆμƒ 좜λ ₯

두 번째 ν”Œλ‘―μ΄μ΄ 예제의 첫 번째 ν”Œλ‘―κ³Ό λ™μΌν•˜κ²Œ μž‘λ™ ν•  κ²ƒμœΌλ‘œ μ˜ˆμƒν•©λ‹ˆλ‹€.

pd.show_versions() 좜λ ₯

INSTALLED VERSIONS
------------------
commit: None
python: 3.5.2.final.0
python-bits: 64
OS: Linux
OS-release: 4.4.6-gentoo
machine: x86_64
processor: Intel(R) Core(TM) i3-3240 CPU @ 3.40GHz
byteorder: little
LC_ALL: None
LANG: en_US.utf8

pandas: 0.18.1
nose: 1.3.7
pip: 8.1.2
setuptools: 25.1.6
Cython: None
numpy: 1.11.1
scipy: 0.18.0
statsmodels: 0.6.1
xarray: None
IPython: 5.1.0
sphinx: 1.4.1
patsy: 0.4.1
dateutil: 2.5.3
pytz: 2016.6.1
blosc: None
bottleneck: None
tables: 3.2.3.1
numexpr: 2.6.1
matplotlib: 1.5.1
openpyxl: None
xlrd: 1.0.0
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.0.13
pymysql: None
psycopg2: 2.6.1 (dt dec pq3 ext)
jinja2: 2.8
boto: None
pandas_datareader: None

(FWIW, 방금 μ΅œμ‹  λ²„μ „μ˜ pandasλ₯Ό λΉŒλ“œν•˜κ³  μ‹œλ„ν–ˆλŠ”λ° λ™μΌν•œ λ™μž‘μ΄ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€.

pandas: 0.18.1+391.g6645b2b

)

Usage Question

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

axes ax=ax 을 (λ₯Ό) μ‚¬μš©ν•˜λ €κ³ ν•©λ‹ˆλ‹€.

λΆˆν–‰νžˆλ„ μ΄λŸ¬ν•œ μ’…λ₯˜μ˜ 였λ₯˜λ₯Ό 작기 μœ„ν•΄ ν•  μˆ˜μžˆλŠ” 일이 λ§Žμ§€ μ•Šλ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€. .plot λ©”μ„œλ“œμ—μ„œ **kwargs λ₯Ό λ°›μ•„ κΈ°λ³Έ matplotlib λ©”μ„œλ“œ (예 : .ax.bar )에 μ „λ‹¬ν•©λ‹ˆλ‹€. μ™œ matplotlib axes.bar μ—μ„œ pylab μΈν„°νŽ˜μ΄μŠ€μ™€μ˜ ν˜Έν™˜μ„±μ„ μœ„ν•΄ axes ν‚€μ›Œλ“œλ₯Ό ν—ˆμš©ν•˜λŠ”μ§€ 잘 λͺ¨λ₯΄κ² μŠ΅λ‹ˆλ‹€.

λͺ¨λ“  3 λŒ“κΈ€

axes ax=ax 을 (λ₯Ό) μ‚¬μš©ν•˜λ €κ³ ν•©λ‹ˆλ‹€.

λΆˆν–‰νžˆλ„ μ΄λŸ¬ν•œ μ’…λ₯˜μ˜ 였λ₯˜λ₯Ό 작기 μœ„ν•΄ ν•  μˆ˜μžˆλŠ” 일이 λ§Žμ§€ μ•Šλ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€. .plot λ©”μ„œλ“œμ—μ„œ **kwargs λ₯Ό λ°›μ•„ κΈ°λ³Έ matplotlib λ©”μ„œλ“œ (예 : .ax.bar )에 μ „λ‹¬ν•©λ‹ˆλ‹€. μ™œ matplotlib axes.bar μ—μ„œ pylab μΈν„°νŽ˜μ΄μŠ€μ™€μ˜ ν˜Έν™˜μ„±μ„ μœ„ν•΄ axes ν‚€μ›Œλ“œλ₯Ό ν—ˆμš©ν•˜λŠ”μ§€ 잘 λͺ¨λ₯΄κ² μŠ΅λ‹ˆλ‹€.

μ§€κΈˆμ€μ΄ 뢀뢄을 λ‹«κ² μŠ΅λ‹ˆλ‹€. μš°λ¦¬λŠ” 잠재적으둜 λ°›μ•„ 듀일 수 axes μœ„ν•œ λ³„μΉ­μœΌλ‘œ ax (μ™œ μžˆλŠ”μ§€ ax μ›λž˜ μ„ νƒλ˜μ—ˆλ‹€),ν•˜μ§€λ§Œ μ§€κΈˆμ€ 보λ₯˜ 말을 λ§Žμ€ μ‚¬λžŒλ“€μ΄ μ‹€ν–‰ν•˜λŠ” 경우 λ³Ό 것 μ΄κ²ƒμœΌλ‘œ.

Ack. 감사!

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰