解决Jupyter导入cufflinks的‘The plotly.plotly module is deprecated’错误及绘图替代方案
Hey there! Let's break down how to fix that annoying deprecation error and cover other solid ways to plot DataFrames with Plotly.
一、修复"The plotly.plotly module is deprecated"错误
This error pops up because cufflinks still relies on the old plotly.plotly module, which Plotly has officially retired—all its functionality moved to the chart-studio package. Here's how to fix it:
Install the chart-studio package
Run this command in your Jupyter Notebook to get the required package:!pip install chart-studioAdjust your import code
Reorder your imports to point cufflinks to the new chart-studio module instead of the deprecated one:# Import the updated Plotly module first import chart_studio.plotly as py import cufflinks as cf # For offline plotting (no need to upload to Plotly's cloud), enable offline mode cf.go_offline() # If you want to save charts to Plotly's cloud, add your account credentials (replace with your own) # py.sign_in('your_plotly_username', 'your_plotly_api_key')That's it—no more deprecation warnings when importing cufflinks.
二、其他用Plotly绘制DataFrame的方法
Cufflinks isn't the only game in town. Plotly has several more flexible, widely used ways to visualize DataFrames:
1. Plotly Express (Most Recommended: Simple & Powerful)
Plotly Express is Plotly's high-level API—super intuitive, and it works seamlessly with DataFrames. You can create most common charts in one line:
import plotly.express as px import pandas as pd # Sample DataFrame df = pd.DataFrame({ 'Year': [2020, 2021, 2022, 2023], 'Revenue': [120000, 180000, 150000, 220000], 'Region': ['East', 'West', 'East', 'West'] }) # Line chart with automatic color coding by region line_fig = px.line(df, x='Year', y='Revenue', color='Region', title='Annual Revenue by Region') line_fig.show() # Grouped bar chart bar_fig = px.bar(df, x='Year', y='Revenue', color='Region', barmode='group') bar_fig.show()
It supports line charts, bar charts, scatter plots, histograms, box plots, and more—auto-handles legends, labels, and interactivity out of the box.
2. Plotly Graph Objects (Full Customization)
If you need fine-grained control over every detail of your chart (colors, styles, layouts), use Plotly's low-level Graph Objects API:
import plotly.graph_objects as go import pandas as pd df = pd.DataFrame({ 'Product': ['A', 'B', 'C', 'D'], 'Sales': [300, 250, 400, 320] }) # Create a bar chart with custom colors fig = go.Figure(data=[go.Bar( x=df['Product'], y=df['Sales'], marker_color=['#FF6B6B', '#4ECDC4', '#45B7D1', '#96CEB4'] )]) # Tweak the layout to your liking fig.update_layout( title='Product Sales Comparison', xaxis_title='Product Name', yaxis_title='Units Sold', plot_bgcolor='rgba(0,0,0,0)' # Transparent background ) fig.show()
This is perfect for building complex, tailored visualizations that stand out.
3. Pandas + Plotly Backend
If you love Pandas' built-in plot() syntax, you can switch its backend to Plotly to get interactive charts without learning new code:
import pandas as pd # Set Plotly as Pandas' default plotting backend pd.options.plotting.backend = "plotly" df = pd.DataFrame({ 'X': [1,2,3,4,5], 'Y1': [2,4,5,4,2], 'Y2': [1,3,4,3,1] }) # Use Pandas' plot() method as usual—now it generates Plotly charts! fig = df.plot(kind='line', title='Dual Line Comparison') fig.show()
Great for anyone who prefers the familiar Pandas workflow but wants interactive, shareable charts.
内容的提问来源于stack exchange,提问作者Raj Verma

