【问题标题】:How do I fill a Pyplot Line plot and change the fill depended on the value如何填充 Pyplot 线图并根据值更改填充
【发布时间】: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) 值对右侧线图进行颜色缩放。 以下是我正在尝试创建的内容。

我想要创造的是这个。 5

【问题讨论】:

    标签: python pandas dataframe matplotlib


    【解决方案1】:

    经过很多死胡同,我想出了以下代码。我已经订购了填充物(没有 alpha),因此它们彼此叠放,有效地掩盖了底层填充物。可能不优雅,但完成工作。

    axs[1].fill_betweenx(df['Depth'],0,df['SBTn'], where=(df['SBTn'])>=1,facecolor='red')
    axs[1].fill_betweenx(df['Depth'],0,df['SBTn'], where=(df['SBTn'])>=2,facecolor='#dc7633')
    axs[1].fill_betweenx(df['Depth'],0,df['SBTn'], where=(df['SBTn'])>=3,facecolor='#5f5b95')
    axs[1].fill_betweenx(df['Depth'],0,df['SBTn'], where=(df['SBTn'])>=4,facecolor='#45b39d')
    axs[1].fill_betweenx(df['Depth'],0,df['SBTn'], where=(df['SBTn'])>=5,facecolor='#76d7c4')
    axs[1].fill_betweenx(df['Depth'],0,df['SBTn'], where=(df['SBTn'])>=6,facecolor='#f0b27a')
    axs[1].fill_betweenx(df['Depth'],0,df['SBTn'], where=(df['SBTn'])>=7,facecolor='#e59866')
    

    结果如下。过渡并不尖锐(逐步或垂直于轴),但由于它们代表土壤条件从一个样本到下一个样本的变化,所以斜率是可以的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-04
      • 1970-01-01
      • 2023-02-05
      • 2013-04-12
      相关资源
      最近更新 更多