使用Python垂直线创建数据可视化

2024-06-07 16:47:52 发布

您现在位置:Python中文网/ 问答频道 /正文

大家好,我有一个关于使用python创建数据可视化的问题。 我得到了如下数据

        index   trk  rbegin  rend      suffix  prefix    id
        0     STWOS  130.30  129.00    NaN      H        1
        1   missing  129.00  128.00    NaN      H        1
        2     STWOS  128.00  125.61    NaN      H        1
        3   missing  125.61  125.00    NaN      H        1
         4      STWS  125.00  121.37    NaN      H        1
        5     STWOS  121.37  114.50    NaN      H        1
        6     DTWOS  114.50  106.84    NaN      H        1
       7     STWOS  106.84  101.50    NaN      H        1
      8      STWS  101.50   98.57    NaN      H        1
     9     STWOS   98.57   92.18    NaN      H        1
     10     STWS   92.18   90.25    NaN      H        1
     11    STWOS   90.25   86.00    NaN      H        1
     12    DTWOS   86.00   81.00    NaN      H        1
    13    STWOS   81.00   78.59    NaN      H        1
     14     STWS   78.59   75.07    NaN      H        1
   15    STWOS   75.07   69.71    NaN      H        1
    16     STWS   69.71   67.78    NaN      H        1
     17    STWOS   67.78   60.60    NaN      H        1
      18     STWS   60.60   58.00    NaN      H       1
     19    STWOS   58.00   55.40    NaN      H        1
      20     STWS   55.40   53.31    NaN      H        1
     21    STWOS   53.31   47.96    NaN      H        1
     22     STWS   47.96   45.00    NaN      H        1
     23    STWOS   45.00   43.05    NaN      H        1
     24     STWS   43.05   40.29    NaN      H        1

基本上,我正在尝试创建一个图表。这是一个从rbegin到rend的垂直排列。 如果trk是STWOS,那么它只是从rbegin到rend的一条黑线。如果trck缺失,则为黑色虚线。如果trk为dtwos,则为两条平行的黑线

enter image description here

但是我不能得到太多看起来我得到了这个

enter image description here

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.axes as ax


df1=pd.read_csv("line3.csv")
egs=df1["line_id"]
egs=df1["line_id"]
#for the column return only unique values
egs2=egs.unique()
b=1
a=df1[df1["line_id"]==b]
a=a.sort_values(by="rbegin",ascending=False)
a1=a.drop(["Unnamed: 0","index"],axis=1)
c=max(a1["rbegin"])
d=max(a1["rend"])
z=max(a1["rbegin"])
z2=min(a1["rend"])
ogig=z-z2
r=max(c,d)
a2=a1.to_numpy()
for i in range(0,len(a2)):
    plt.axis(xmin=1,xmax=3)
    yTick=np.arange(0,r,5)
    plt.title("line:"+str(b))
    #plt.yticks(yTick)
    if a2[i][0]=="STWOS":
        plt.vlines(2,a2[i][1],a2[i][2],linewidth=1,color="black")
    if a2[i][0]=="DTWOS":
            plt.annotate(str(a2[i][2]),(1.85,a2[i][2]),fontsize=5)
            plt.vlines(2,a2[i][1],a2[i][2],linewidth=1,color="black")
            plt.vlines(1.95,a2[i][1],a2[i][2],linewidth=1,color="black")
    if a2[i][0]=="DTWS":
        plt.vlines(2,a2[i][1],a2[i][2],linewidth=1,color="black")
        plt.vlines(2.05,a2[i][1],a2[i][2],linewidth=1/2,color="red")
        plt.axhline(y=a2[i][1],xmin=.51,xmax=.53,linewidth=1/7,color="red")
        plt.axhline(y=a2[i][2],xmin=.51,xmax=.53,linewidth=1/7,color="red")
    if a2[i][0]=="missing":
        plt.annotate(str(a2[i][2]),(1.85,a2[i][2]),fontsize=5)
        plt.vlines(2,a2[i][1],a2[i][2],color="black",linewidth=1,linestyles="dashed")
    if a2[i][0]=="STWS":
        plt.annotate(str(a2[i][2]),(1.85,a2[i][2]),fontsize=5)
        plt.vlines(2.05,a2[i][1],a2[i][2],linewidth=1/2,color="red")
        plt.axhline(y=a2[i][1],xmin=.51,xmax=.53,linewidth=1/7,color="red")
        plt.axhline(y=a2[i][2],xmin=.51,xmax=.53,linewidth=1/7,color="red")
        plt.vlines(2,a2[i][1],a2[i][2],linewidth=1,color="black")
fig = plt.figure()
plt.figure().clear()
plt.close()
plt.cla()
plt.clf()








Tags: a2a1pltrednancolorblackdf1
1条回答
网友
1楼 · 发布于 2024-06-07 16:47:52

垂直线、水平线和注释被制成函数,以消除代码中的冗余。我们还使y轴刻度更为详细。此外,由于我们将数据作为数据帧处理,因此我们已改为按原样使用它。此外,如果有多个ID,则需要添加循环过程并将x轴位置值添加到绘图函数中

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.axes as ax

def draw_vline(ax, r, x, l, c, ls='solid'):
    ax.vlines(x, r[1], r[2], lw=l, color=c, ls=ls)

def draw_hline(ax, r, l, c):
    ax.axhline(y=r[1], xmin=0.52, xmax=0.54, lw=l, color=c)
    ax.axhline(y=r[2], xmin=0.52, xmax=0.54, lw=l, color=c)
    
def annot(ax, r):
     ax.annotate(str(r[2]),(0.1,r[2]), fontsize=5)
    
fig,ax = plt.subplots(figsize=(1,8))
ax.axis(xmin=0, xmax=1)
x1,x2,x3 = 0.5,0.45,0.55
b=1

for idx, row in df1.iterrows():
    ax.set_title("line:"+ str(b))
    if row[0]=="STWOS":
        draw_vline(ax, row, x1, 1, 'k')
    if row[0]=="DTWOS":
        annot(ax, row)
        draw_vline(ax, row, x1, 1, 'k')
        draw_vline(ax, row, x2, 1, 'k')
    if row[0]=="DTWS":
        draw_vline(ax, row, x1, 1, 'k')
        draw_vline(ax, row, x3, 1/2, 'r')
        draw_hline(ax, row, 1/4, 'r')
    if row[0]=="missing":
        annot(ax, row)
        draw_vline(ax, row, x1, 1, 'k', ls='dashed')
    if row[0]=="STWS":
        annot(ax, row)
        draw_vline(ax, row, x3, 1/2, 'r')
        draw_hline(ax, row, 1/4, 'r')
        draw_vline(ax, row, x1, 1, 'k')
            
ax.set_xlim(0,1)
ax.set_ylim(40,130)
ax.set_yticks(np.arange(40.0,131.0,2.5))
ax.grid(color='0.9', lw=0.5, ls='dashed')

plt.show()

相关问题 更多 >

    热门问题