Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

I am plotting a graph by reading a csv file. I wanted to convert ticks of 1st-axis into 2nd x-axis by the conversion 1239.8/x-original tick

$
0
0
import osimport csvimport matplotlib.pyplot as plt        # Plotting for each CSV file        for i in range(len(x_values)):            plt.plot([x_values[i], x_values[i]], [y_values[i], 0], marker='', linestyle='-', color='black')            plt.annotate(f" {state[i]}", (x_values[i], y_values[i]), textcoords="offset points", xytext=(0,5), ha='center')    # Overall Plotting    plt.xlabel('2nd Column')    plt.ylabel('3rd Column')    plt.title('Vertical Lines Connecting 2nd and 3rd Column Values for All CSV Files')    # Create a second x-axis    ax2 = plt.gca().twiny()    # Set ticks for the second x-axis using the reciprocal of the original x-axis values    original_ticks = plt.gca().get_xticks()    ax2_ticks = [1 / val if val != 0 else 0 for val in original_ticks]    # Set ticks and labels for the second x-axis    ax2.set_xticks(original_ticks)    ax2.set_xticklabels([f"{round(1239.8/tick, 2)}" if tick != 0 else '0' for tick in ax2_ticks])    ax2.set_xlabel('Secondary X-Axis (Derived from 2nd Column with Reciprocal)')    plt.show()

I am using this code and trying to plot 2nd x-axis with respect to first x-axis ticks. the conversion I want to achieve is 1239.8/ticks of x-axis. How do I achieve this? I am starting to learn. My data is 3 column in which I give first column as an argument to plot 2nd and 3rd value on graph as straight line or bar graph.


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>