我花了好几个小时试图做我认为是一项简单的任务,即在使用seaborn时将标签添加到XY图上.
这是我的代码
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
df_iris=sns.load_dataset("iris")
sns.lmplot('sepal_length', # Horizontal axis
'sepal_width', # Vertical axis
data=df_iris, # Data source
fit_reg=False, # Don't fix a regression line
size = 8,
aspect =2 ) # size and dimension
plt.title('Example Plot')
# Set x-axis label
plt.xlabel('Sepal Length')
# Set y-axis label
plt.ylabel('Sepal Width')
Run Code Online (Sandbox Code Playgroud)
我想在图中的每个点添加"种类"栏中的文字.
我见过许多使用matplotlib但不使用seaborn的例子.
有任何想法吗?谢谢.
我有一个dt
:
>>> dt
sales mg
ID 600519 600809 600519 600809
RPT_Date
20060331 13.5301 5.8951 9.4971 3.0408
20060630 6.6048 2.4081 4.3088 1.4040
20060930 12.3889 3.6053 9.1455 2.0754
20061231 16.5100 3.3659 12.4682 1.8810
20070331 15.8754 5.9129 11.5833 3.6736
20070630 10.4155 3.5759 7.8966 2.2812
20070930 18.2929 3.5280 14.3552 2.1584
20071231 27.7905 5.4510 23.7820 3.2568
Run Code Online (Sandbox Code Playgroud)
并使用pandas函数plot
创建一个barplot对象
>>> fig = dt.plot(kind='bar', use_index=True)
>>> fig
<matplotlib.axes.AxesSubplot object at 0x0B387150>
Run Code Online (Sandbox Code Playgroud)
但是我想要一个<matplotlib.figure.Figure object>
intsead传递给其他函数,就像下面的对象类型一样:
>>> plt.figure()
<matplotlib.figure.Figure object at 0x123A6910>
Run Code Online (Sandbox Code Playgroud)
所以,我怎么能转换<matplotlib.axes.AxesSubplot object> …
我的代码
import matplotlib.pyplot as plt
plt.style.use("ggplot")
import numpy as np
from mtspec import mtspec
from mtspec.util import _load_mtdata
data = np.loadtxt('262_V01_C00_R000_TEx_BL_4096H.dat')
spec,freq,jackknife,f_statistics,degrees_of_f = mtspec(data=data, delta= 4930.0, time_bandwidth=4 ,number_of_tapers=5, nfft= 4194304, statistics=True)
fig = plt.figure()
ax2 = fig
ax2.plot(freq, spec, color='black')
ax2.fill_between(freq, jackknife[:, 0], jackknife[:, 1],color="red", alpha=0.3)
ax2.set_xlim(freq[0], freq[-1])
ax2.set_ylim(0.1E1, 1E5)
ax2.set_xlabel("Frequency $")
ax2.set_ylabel("Power Spectral Density $)")
plt.tight_layout()
plt.show()
Run Code Online (Sandbox Code Playgroud)
问题在于我的代码的绘图部分.我应该改变什么?我在Ubuntu上使用Python 2.7.
我检查了这个Matplotlib返回一个绘图对象,但它确实不适合我的问题。
我想做的是:
def func1():
fig1 = plt.plot (np.arange(0.0, 5.0, 0.1))
return fig1
def func2()
return plt.plot (np.arange(0.0, 5.0, 0.02))
fig1 = func1()
fig2 = func2()
plt.figure()
plt.add_subplot(fig1)
plt.add_subplot(fig2)
plt.show()
Run Code Online (Sandbox Code Playgroud)
上面的代码只是一个主要思想。你能建议我怎么做吗?
谢谢