Search code examples
pythonaltair

How to plot line plot with vertical-based data (well-log)?


I was trying to plot geophysics data (well-log) into a scatter plot in Altair using mark_line function, but the line plot is not connecting the dots/ points from top-bottom, but rather from left-right. If you see figure on the left, the data is distributed vertically as clearly seen, in the middle is the result using mark_line, and on the right is the one I wanted, just flipped the X and Y axis.

Is there any way to make a plot to behave just like left figure, but in line encoding?

Or perhaps some form of hacks to flipped the display on the right figure?

chart1 = alt.Chart(w).mark_point(color='green').encode(
  alt.X('GR', scale=alt.Scale(domain=[0,300])),
  alt.Y('DEPT', scale=alt.Scale(domain=[7000, 7100])),
).interactive()


chart2 = alt.Chart(w).mark_line(color='green').encode(
  alt.X('GR', scale=alt.Scale(domain=[0,300])),
  alt.Y('DEPT', scale=alt.Scale(domain=[7000, 7100])),
).interactive()



chart3 = alt.Chart(w).mark_line(color='green').encode(
  alt.Y('GR', scale=alt.Scale(domain=[0,300])),
  alt.X('DEPT', scale=alt.Scale(domain=[7000, 7100])),
).interactive()

chart1 | chart2 | chart3


Plot using Altair

For those who needs more information, this is a typical dataset from borehole geophysics data/ well-log. Data (GR) is displayed in vertical line, against depth (DEPT).

Thanks for the help!


Solution

  • From what I tested so far, Altair scatters plot using mark_line will always follow the X-axis by default. Therefore, in the case where you want to plot data across Y-axis, one has to specify the order of the connecting line. In the following, I add order = 'DEPT' which was the Y-axis in the plot.

    alt.Chart(
      w
    
    ).mark_line(
      color='green', 
      point=True,
    
    ).encode(
      alt.X('GR', scale=alt.Scale(domain=[0,250])),
      alt.Y('DEPT', sort = 'descending',scale=alt.Scale(domain=[7000, 7030])),
      order = 'DEPT' #this has to be added to make sure the plot is following the order of Y-axis, DEPT
    
    ).configure_mark(
      color = 'red'
    
    ).interactive()
    
    

    Result:

    Scatter plot with vertical data using Altair