Plotly在Spyder(Python 3.5)不显示图表及热力图代码运行问题
Hey there! Let's work through the two main issues you're facing: getting your Plotly heatmap code up and running, and getting it to display properly in Spyder.
1. Fixing Your Plotly Code & Account Setup
First, let's sort out your code. A couple key points to note:
- Newer Plotly versions (v4+) let you run plots offline without an account—this is often simpler for beginners who just want to generate local charts.
- If you do want to use your online Plotly account, you need to properly configure your credentials and finish the incomplete code.
Option 1: Offline Mode (No Account Required)
This is the easiest way to get started. Adjust your code like this:
import plotly.graph_objs as go from plotly.offline import plot, init_notebook_mode, iplot # Create your heatmap trace trace = go.Heatmap(z=[[1, 20, 30],[20, 1, 60],[30, 60, 1]]) data = [trace] # Option A: Generate an HTML file that opens in your default browser plot(data, filename='local_heatmap.html') # Option B: Try inline rendering in Spyder (we'll tweak this more below) init_notebook_mode(connected=True) iplot(data)
Option 2: Online Mode (With Your Plotly Account)
If you want to save plots to your online account, uncomment the credentials line and complete the py.plot() call:
import plotly # Uncomment and use your actual account details plotly.tools.set_credentials_file(username='julirov', api_key='your_real_api_key_here') import plotly.plotly as py import plotly.graph_objs as go trace = go.Heatmap(z=[[1, 20, 30],[20, 1, 60],[30, 60, 1]]) data = [trace] # This saves the plot to your Plotly account and opens it in the browser py.plot(data, filename='online_heatmap')
2. Getting Plotly Charts to Display in Spyder
Spyder has some specific rendering quirks—try these fixes one by one:
Fix 1: Force Plotly to Use Spyder-Compatible Renderer
Add these lines at the top of your script to set the correct renderer:
import plotly.io as pio # Try 'spyder' first; if that fails, use 'inline' pio.renderers.default = 'spyder'
Fix 2: Adjust Spyder's Graphics Backend
- Open Spyder, go to Tools > Preferences > IPython Console > Graphics
- Under "Backend", select Qt5 or Automatic (Qt5 is usually more reliable for Plotly)
- Restart Spyder to apply the change
Fix 3: Fallback to Browser Display
If inline rendering still doesn't work, use the offline plot() function from Option 1 above—it generates an HTML file and opens it in your default browser, which should always work.
内容的提问来源于stack exchange,提问作者Julian Rovner

