gpt4 book ai didi

python - 如何填充 Pyplot 线图并根据值更改填充

转载 作者:太空宇宙 更新时间:2023-11-03 20:00:55 25 4
gpt4 key购买 nike

我有一个 pd DataFrame,其中包含深度列和这些深度点处的变量的许多其他列。绘制折线图很好。我想要做的是根据类别(整数,1-6)绘制深度,并根据类别更改条形或填充。我还希望能够与相邻的线型子图共享=true。我尝试过使用水平条,但没有考虑深度,因为 BarH 只是连续的(在本例中,我的深度数据仅达到 0.064)。 This is a BarH adjacent to my Depth/Category plot

我还尝试绘制深度/类别,试图跨越其中,但成功有限。我可以让它使用随机生成的数据,但当我将 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()

这是数据的片段。我正在绘制深度/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

This is my plot so far, i am trying to color scale the right-hand plot

上面是我的图,我想根据类别 (SBTno) 值对右侧线图进行颜色缩放。以下是我正在尝试创建的内容。

我正在尝试创建的是这个。 Where i want to get to 5

最佳答案

经过很多死胡同,我想出了以下代码。我已经订购了填充(没有 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')

结果如下。过渡并不尖锐(逐步或垂直于轴),但由于它们代表从一个样本到下一个样本的土壤状况变化,因此斜率是可以的。 finished (maybe) product.

关于python - 如何填充 Pyplot 线图并根据值更改填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59239174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com