0
  • I have a dataframe (97 columns x 30 rows). In this dataframe there are only 1 and 0.
  • I want to plot it like a scatter plot, in which in the x axis the are the name of the columns and in the y axis the name of the indexes.

[my dataframe is like this][1]

  • The output I want is similar to the photo, but the red dot must be there only if the value of the intersection between row and columns has a 1 value.
  • If there is a 0 value nothing is plot in the intersection.[][2][the output scatter plot I want][3]
  1. https://i.sstatic.net/hFnQX.png
  2. https://i.sstatic.net/Rsguk.jpg
  3. https://i.sstatic.net/keGC6.png
4
  • please dont post the image but post the code Commented Jul 29, 2020 at 17:15
  • what is your desired output Commented Jul 29, 2020 at 17:15
  • oh, there is no code yet, just two lines to read the dataframe xD The output is the scatter plot with red dots Commented Jul 29, 2020 at 17:18
  • Is it possible to post the data of the data frame and make it accessible to us? Commented Jul 29, 2020 at 18:35

1 Answer 1

1

A straightforward way to do this is to use two nested loops for plotting the points conditionally on each dataframe cell:

import pandas as pd
import matplotlib.pyplot as plt

example = pd.DataFrame({'column 1': [0, 1, 0, 1], 
                        'column 2': [1, 0, 1, 0],
                        'column 3': [1, 1, 0, 0]})

for x, col in enumerate(example.columns):
    for y, ind in enumerate(example.index):
        if example.loc[ind, col]:
            plt.plot(x, y, 'o', color='red')
            
plt.xticks(range(len(example.columns)), labels=example.columns)
plt.yticks(range(len(example)), labels=example.index)
    
plt.show()

example plot

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.