简体   繁体   中英

I want to swap x-axis and y-axis and change this graph to horizontal graph python

I want to swap the x-axis and the y-axis and turn this graph into a horizontal graph

my python code

    an_image = PIL.Image.open("static/img/t2.jpg")
    gray_img = ImageOps.grayscale(an_image)
    image_sequence = gray_img.getdata()
    image_array = np.array(image_sequence)
    with plt.style.context('dark_background'):
        plt.plot(image_array[:1024], color='white' )
    plt.savefig('static/img/his.png')

I'm not sure if that's what you want, but this will swap x-axis and y-axis of the original graph:

an_image = PIL.Image.open("static/img/t2.jpg")
gray_img = ImageOps.grayscale(an_image)
image_sequence = gray_img.getdata()

y = np.array(image_sequence)
with plt.style.context('dark_background'):
    # Create the x-axis with the same size as y-axis
    x = np.arange(0, 1024)
    
    # Plot x-axis and y-axis in swapped positions
    plt.plot(y[:1024], x, color='white')

plt.savefig('static/img/his.png')

If I want to move x-axis to the top What do I have to do?

Just add

plt.gca().xaxis.tick_top()

before saving the fig.

Full code:

an_image = PIL.Image.open("static/img/t2.jpg")
gray_img = ImageOps.grayscale(an_image)
image_sequence = gray_img.getdata()

y = np.array(image_sequence)
with plt.style.context('dark_background'):
    x = np.arange(0, 1024)
    plt.plot(y[:1024], x, color='white')

plt.gca().xaxis.tick_top()
plt.savefig('static/img/his.png')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM