2

Here is my code:

import pandas as pd
import matplotlib.pyplot as plt

wine = pd.read_csv('red wine quality.csv')

wine = wine.dropna()

plt.figure()
wine.plot.scatter(x = 'pH', y = 'alcohol', c = 'quality', alpha = 0.4,\
                  cmap = plt.get_cmap('jet'), colorbar = True)
plt.savefig('scatter plot.png')
plt.tight_layout()
plt.show()

Here is the plot that I get:

enter image description here

I get a scatter plot with the y-axis labeled as 'alcohol' ranging from 9-15 and the color bar labeled as 'quality' ranging from 3-8. I thought I had designated in my code that the x-axis would show up labeled as 'pH', but I get nothing. I have tried adjusting my figsize down to [8, 8], setting dpi to 100, and labeling the axes, but nothing will make the x-axis show up. What am I doing wrong?

Here is an MCVE to play with:

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

df = pd.DataFrame(np.random.randn(50, 4), columns=['A', 'B', 'C', 'D'])

plt.figure()
df.plot.scatter(x = 'A', y = 'B', c = 'C', alpha = 0.4,\
                  cmap = plt.get_cmap('jet'), colorbar = True)
plt.tight_layout()
plt.show()
Mr. T
  • 11,960
  • 10
  • 32
  • 54
Renzzy
  • 25
  • 1
  • 8
  • What is wine?.. – Mad Physicist Mar 02 '21 at 18:31
  • 1
    Please post an MCVE. A randomly generated DF snippet is fine – Mad Physicist Mar 02 '21 at 18:33
  • "wine" is a pandas df; I have adjusted my code sample to include that info. I am using matplotlib version 3.3.2, and pandas version 1.1.3. I have included an MCVE in the post as well. – Renzzy Mar 02 '21 at 18:50
  • Are you using https://www.kaggle.com/uciml/red-wine-quality-cortez-et-al-2009?select=winequality-red.csv ? – JohanC Mar 02 '21 at 18:52
  • 2
    Your problem is not reproducible. But you should leave out `plt.figure()` to avoid creating an empty plot. Best create the plot as `fig, ax = plt.subplots()` and use `wine.plot.scatter(...., ax=ax)`. Are you running this inside a Jupyter notebook? Did you try `savefig(..., bbox_inches='tight)`? – JohanC Mar 02 '21 at 18:57
  • Running in Spyder, and sadly I reproduce it every time I run the cell, with both the wine df and with the MCVE random numpy df. I admit I am not familiar with this alternate method of creating a scatter plot, though. I did try your adding bbox_inches='tight' to no avail. – Renzzy Mar 02 '21 at 19:01
  • 2
    I can reproduce this in Spyder, but specifically using `ax=ax` fixes the issue. – BigBen Mar 02 '21 at 19:04
  • 2
    @JohanC already posted it. Add `fig, ax = plt.subplots()`, then add `ax=ax` to your `scatter` call. And get rid of `plt.figure()`. – BigBen Mar 02 '21 at 19:13
  • 1
    I read both comments more carefully and was able to get the x-axis tick labels to show using the methods you recommended. My baby coder eyes didn't pick up what you were laying down at first, but I've got it now. Thank you very much! – Renzzy Mar 02 '21 at 19:16

1 Answers1

3

X axis label and minor tick labels not showing on Pandas scatter plot is a known and open issue with Pandas (see BUG: Scatterplot x-axis label disappears with colorscale when using matplotlib backend · Issue #36064).

This bug occurs with Jupyter notebooks displaying Pandas scatterplots that have a colormap while using Matplotlib as the plotting backend. The simplest workaround is passing sharex=False to pandas.DataFrame.plot.scatter.

See Make pandas plot() show xlabel and xvalues

marianoju
  • 334
  • 4
  • 15