【发布时间】:2019-12-08 19:35:10
【问题描述】:
我有一个 pd DataFrame,它包含一个 Depth 列和许多其他列,用于这些深度点的变量。绘制折线图很好。我想要做的是根据类别(整数,1-6)绘制深度,并根据类别更改条形或填充。我还希望能够 sharey=true 与相邻的线型子图。 我尝试过使用水平条,但没有考虑深度,因为 BarH 只是顺序的(在本例中,我的深度数据仅达到 0.064)。
我也尝试绘制深度/类别试图跨度,但成功有限。我可以让它与随机生成的数据一起工作,但当我将 csv 读入我的 DataFrame 时却不行。
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.collections as collections
data= pd.read_csv (r'/Users/????/Desktop/snippit.csv')
df=pd.DataFrame(data, columns=['Depth','SBTno'])
df['Depth']*=-1 #invert depths - below seabed
y=df.SBTno #This is the bit that seems to casue the errors
x=df.Depth #Using np genertaed figure works.
fig,ax=plt.subplots()
ax.plot(x,y,color='red')
Z1=1
Z2=2
Z3=3
Z4=4
Z5=5
collection = collections.BrokenBarHCollection.span_where(
x, ymin=0, ymax=Z1,where=x >Z1, facecolor='green',alpha=0.5)
ax.add_collection(collection)
collection = collections.BrokenBarHCollection.span_where(
x, ymin=0, ymax=Z2,where=y >Z2, facecolor='red',alpha=0.5)
ax.add_collection(collection)
collection = collections.BrokenBarHCollection.span_where(
x, ymin=0, ymax=Z3,where=y >Z3, facecolor='blue',alpha=0.5)
ax.add_collection(collection)
collection = collections.BrokenBarHCollection.span_where(
x, ymin=0, ymax=Z4,where=y >Z4, facecolor='grey',alpha=0.5)
ax.add_collection(collection)
collection = collections.BrokenBarHCollection.span_where(
x, ymin=0, ymax=Z5,where=y >Z5, facecolor='purple',alpha=0.5)
ax.add_collection(collection)
plt.show()
这是数据的 sn-p。我正在绘制深度/SBTno(整数,1-5)
Depth,Cone,Friction,SBTno
0,0,0,0
0.001,0.012,0.003,2
0.005,0.02,0.003,2
0.009,0.044,0.003,3
0.013,0.052,0.003,4
0.017,0.071,0.004,5
0.021,0.129,0.004,4
0.025,0.193,0.004,4
0.028,0.265,0.005,3
0.033,0.408,0.005,2
0.036,0.624,0.005,1
0.04,0.898,0.005,4
0.044,1.36,0.005,4
0.048,1.68,0.006,4
0.052,2.047,0.006,3
0.056,2.209,0.006,5
0.06,2.249,0.007,2
0.064,2.217,0.007,2
上面是我的图,我想根据类别 (SBTno) 值对右侧线图进行颜色缩放。 以下是我正在尝试创建的内容。
【问题讨论】:
标签: python pandas dataframe matplotlib